The I-III section deals only with bash ...
For others, see section IV ... I: The use of obase, IBase and BC: IBase is the input of numbers, and obase is the output of the number of the system. Well remember, I am input,o is output.
Note that the F of the 16 binary number is capitalized, otherwise the result is 0, which is wrong.
An example is provided:
10-in-turn 2-in-system:
10 into the 16 system: (If the input is 10, ibase can be omitted)
16-in-turn 2-in-system:
16 into 10: (at this point, just specify IBase)
II: Using Echo's base#:
$ ((base#number))
#其中number是欲转化的数, Base is its number of systems. (Base can also be omitted, number is decimal by default, if 0, or 0x is octal and hexadecimal respectively). #echo $ ((012)) #8进制, the result is #echo $ ((0xFF)) #16进制, the result is 255 #echo $ ((0xEF)) #echo $ ((0xFFFF)) #echo $ ((2#1101011)) #echo $ ((2#11111111)) #2进制, the result is 255 #echo $ (8#255) #8进制, the result is 173 #echo $ ((8#377)) #16进制, and the result is 217 #echo $ ((16#D9)) #echo $ (16#FF)) #有base不用0x, otherwise the error is not correct. 8 is no problem!
In addition, this usage can be extended:
$ ((base#number^base#base1)) #可以理解为number和Base1这两者按照base进制做了个异或操作.
For example: #base =1f #Base1 =ff #echo $ (16# $base ^16# $Base 1)) 224 #这个例子就表示, Base (1f) and BASE1 (FF) are different or manipulated according to 16 binary (the result is E0, that is 224) c1> III: examples:
question:
Now you want to do or compute two binary data, such as
A= 1101
b= 1000
C= a|b
Output c = 1101 in binary format???
Answer: [bob@hzling08:~]-no.96-$ a=1101; b=1000 [bob@hzling08:~]-no.97-$ d= ' echo $ ((2# $a)) |$ ((2# $b))) | BC ' [bob@hzling08:~]-no.98-$ Echo ' obase=2; $d "| BC 1101
can actually write a sentence: [Bob@hzling08:~]-no.99-$ Echo obase=2;$ ((2# $a)) |$ ((2# $b))) "| BC 1101 IV: add:
1. Use awk: #十六进制转十进制 echo 0x2a | gawk ' $1=strtonum ($) #科学计数法转十进制 printf "%f\n" 3.1188622400e+06 Echo 3.1188622400e+06 | awk ' {printf ("%f\n", $)} ' #科学计数法转十进制并保留两位小数 echo 3.1188622400e+06 | awk ' {printf ("%.2f\n", $)} ' #科学计数法转十进制并取整 printf "%.f\n" 3.1188622400e+06 echo 3.1188622400e+06 | awk ' {printf ("%d\n", $)} ' #科学计数法转十进制并四舍五入取整 echo 3.1188622400e+06 | awk ' {printf ("%d\n",