Linux shell different binary data conversion (binary, octal, Hex, base64)

Source: Internet
Author: User
Tags base64 string back

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)

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.