Shell script to convert hexadecimal decimal octal binary

Source: Internet
Author: User
Tags binary to decimal
  • 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

 

Related Article

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.