This article reprinted http://www.cnblogs.com/chengmo/archive/2010/10/14/1851570.html, thanks to the author!
The shell can represent different binary data without invoking a 3rd party command. Here is a summary of the presentation methods. The default value of the shell script is handled by a 10 binary number, unless the number is preceded by a particular notation or prefix. To represent other binary type values. such as: Starting with 0 is the 8 binary. Starting with 0x is the 16 binary number. Using Base#number this form can represent other binary. Base value: 2-64.
How to use:
- Other binary into 10 binary
Octal goto Decimal:
[Email protected] ~]$ ((num=0123));
[Email protected] ~]$ echo $num;
83
[Email protected] ~]$ ((num=8#123));
[Email protected] ~]$ echo $num;
83
((expression)), (()), which can be any data expression. If you previously added: "$" You can read the results of the calculation.
Hexadecimal to decimal:
[Email protected] ~]$ ((num=0xff));
[Email protected] ~]$ echo $num;
255
[Email protected] ~]$ ((NUM=16#FF));
[Email protected] ~]$ echo $num;
255
base-32 Turn decimal:
[Email protected] ~]$ ((NUM=32#FFFF));
[Email protected] ~]$ echo $num;
507375
Base64 Turn decimal:
[Email protected] ~]$ ((NUM=64#ABC_));
[Email protected] ~]$ echo $num;
2667327
Binary goto Decimal
[Email protected] ~]$ ((num=2#11111111));
[Email protected] ~]$ echo $num;
255
- Decimal to other binary (this chapter is wrong, this is a hint!) )
Decimal Turn octal
This is used here: the BC External command is complete. The BC command format is converted to: echo "obase=; value" |BC
[Email protected] ~]$ echo "obase=8;01234567" |BC
4553207
Binary, hexadecimal, base64 conversion to decimal is also the same method.
[Email protected] ~]$ echo "obase=64;123456" |BC
30 09 00
Shell, built-in various notation methods are very simple. Remember Base#number can. Remember to use the (()) notation when assigning values. You can't use the = number directly. The = number does not have a value type. The default is to turn the string back on. Such as:
[Email protected] ~]$ num=0123;
[Email protected] ~]$ echo $num;
0123
The beginning of 0 has lost its meaning.
You can achieve the (()) effect by using the definition: let.
[[email protected] ~]$ let num=0123;
[Email protected] ~]$ echo $num;
83
Linux shell different binary data conversion (binary, octal, Hex, base64)