Examples of different data transformations in shell scripts (binary, octal, 16, base64) _linux Shell

Source: Internet
Author: User
Tags base64 string back

The shell can represent different data in the system without invoking the 3rd party command. The following presentation method is summarized here. The default value of the shell script is handled by the 10 number, unless the number has a special notation or prefix at the beginning. Can be used to represent other types of values in the system. For example: Starting with 0 is 8. The beginning of the 0x is the 16 number. Using Base#number this form can represent other systems. Base value: 2-64.

How to use:

Other systems are converted into 10

Octal Turn decimal:

Copy Code code as follows:

[Chengmo@centos5 ~]$ ((num=0123));
[Chengmo@centos5 ~]$ Echo $num;
83

[Chengmo@centos5 ~]$ ((num=8#123));
[Chengmo@centos5 ~]$ Echo $num;
83

((expression)), (()), the inside can be any data expression. If you add: "$" before, you can read the results of the calculation.

Hexadecimal decimal:

Copy Code code as follows:

[Chengmo@centos5 ~]$ ((num=0xff));
[Chengmo@centos5 ~]$ Echo $num;
255
[Chengmo@centos5 ~]$ ((NUM=16#FF));
[Chengmo@centos5 ~]$ Echo $num;
255

base-32 Turn decimal:

Copy Code code as follows:

[Chengmo@centos5 ~]$ ((NUM=32#FFFF));
[Chengmo@centos5 ~]$ Echo $num;
507375

Base64 Turn decimal:

Copy Code code as follows:

[Chengmo@centos5 ~]$ ((num=64#abc_));
[Chengmo@centos5 ~]$ Echo $num;
2667327

Binary Turn Decimal

Copy Code code as follows:

[Chengmo@centos5 ~]$ ((num=2#11111111));
[Chengmo@centos5 ~]$ Echo $num;
255

Decimal conversion to other systems

Decimal Turn octal

Used here: BC External command complete. BC Command format conversion to: echo "obase=; value" |BC

Copy Code code as follows:

[Chengmo@centos5 ~]$ echo "obase=8;01234567" |BC
4553207

Binary, hexadecimal, base64 conversion to decimal is also the same method.

Copy Code code as follows:

[Chengmo@centos5 ~]$ echo "obase=64;123456" |BC
30 09 00

Shell, built-in various representations of the method is very simple. Remember Base#number can be. Here remember to use the (()) symbol when assigning values. You can't use the = number directly. = number has no value type. The default is to turn the string back. Such as:

Copy Code code as follows:

[Chengmo@centos5 ~]$ num=0123;
[Chengmo@centos5 ~]$ Echo $num;
0123

The beginning of 0 has lost its meaning.

You can achieve (()) an effect by using the definition character: let.

Copy Code code as follows:

[Chengmo@centos5 ~]$ let num=0123;
[Chengmo@centos5 ~]$ Echo $num;
83

There are other better ways, friends can share with me.

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.