Linux shell conversion of different hexadecimal data

Source: Internet
Author: User
Tags binary to decimal

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"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.