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
Author: "dikar cloud mozhu"