Address: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851570.html
Shell can represent different hexadecimal data without calling the 3rd-party command. The following methods are summarized here. The default value of a shell script is a 10-digit number, which can represent other numeric values unless the number starts with a special markup method or prefix. For example, if it starts with 0, it is octal. If it starts with 0x, It is a hexadecimal number. In the form of base # number, it can be used to represent other hexadecimal values. Base Value: 2-64.
Usage:
- Convert other hexadecimal values to hexadecimal values
Octal to decimal:
[Chengmo @ centos5 ~] $ (Num = 0123 ));
[Chengmo @ centos5 ~] $ Echo $ num;
83
[Chengmo @ centos5 ~] $ (Num = 8 #123 ));
[Chengmo @ centos5 ~] $ Echo $ num;
83
(Expression), () can be any data expression. If "$" is added, the calculation result can be read.
Hexadecimal to decimal:
[Chengmo @ centos5 ~] $ (Num = 0xff ));
[Chengmo @ centos5 ~] $ Echo $ num;
255
[Chengmo @ centos5 ~] $ (Num = 16 # ff ));
[Chengmo @ centos5 ~] $ Echo $ num;
255
Base-32 to decimal:
[Chengmo @ centos5 ~] $ (Num = 32 # FFFF ));
[Chengmo @ centos5 ~] $ Echo $ num;
507375
Convert base64 to decimal:
[Chengmo @ centos5 ~] $ (Num = 64 # ABC _));
[Chengmo @ centos5 ~] $ Echo $ num;
2667327
Convert binary to decimal
[Chengmo @ centos5 ~] $ (Num = 2 #11111111 ));
[Chengmo @ centos5 ~] $ Echo $ num;
255
- Convert decimal to other hexadecimal
Decimal to octal
Here, the BC external command is used. BC command format conversion: Echo "obase = hexadecimal; Value" | BC
[Chengmo @ centos5 ~] $ Echo "obase = 8; 01234567" | BC
4553207
Binary, hexadecimal, and base64 are converted to decimal.
[Chengmo @ centos5 ~] $ Echo "obase = 64; 123456" | BC
30 09 00
Shell, built-in various hexadecimal representation methods are very simple. Remember base # number. Remember to use the () symbol when assigning values. You cannot use the = sign directly. = No value type. By default, the backend is changed to a string. For example:
[Chengmo @ centos5 ~] $ Num = 0123;
[Chengmo @ centos5 ~] $ Echo $ num;
0123
It is meaningless to start with 0.
You can use the operator "let" to perform the () operation.
[Chengmo @ centos5 ~] $ Let num = 0123;
[Chengmo @ centos5 ~] $ Echo $ num;
83
There are other better ways to share with me.