Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on the methods of converting different binary data in Shell scripts

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "Shell script in different binary data conversion method tutorial", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Shell script different binary data conversion method tutorial"!

Shell can represent different binary data without invoking third-party commands. The following representations are summarized here. The default value of the shell script is handled by a decimal number unless the number begins with some special notation or prefix. To represent values of other binary types. For example, starting with 0 is octal. Starting with 0x is a hexadecimal number. The form of BASE#NUMBER can be used to represent other systems. base value: 2-64.

How to use it:

Convert the rest of the base to base 10.

Octal to decimal:

The code is as follows:

[chengmo@centos5 ~] $((num=0123))

[chengmo@centos5 ~] $echo $num

eighty-three

[chengmo@centos5 ~] $((num=8#123))

[chengmo@centos5 ~] $echo $num

eighty-three

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

Hexadecimal to decimal:

The code is as follows:

[chengmo@centos5 ~] $((num=0xff))

[chengmo@centos5 ~] $echo $num

two hundred and fifty five

[chengmo@centos5 ~] $((num=16#ff))

[chengmo@centos5 ~] $echo $num

two hundred and fifty five

Base-32 to decimal:

The code is as follows:

[chengmo@centos5 ~] $((num=32#ffff))

[chengmo@centos5 ~] $echo $num

507375

Base64 to decimal:

The code is as follows:

[chengmo@centos5 ~] $((num=64#abc_))

[chengmo@centos5 ~] $echo $num

2667327

Binary to decimal

The code is as follows:

[chengmo@centos5 ~] $((num=2#11111111))

[chengmo@centos5 ~] $echo $num

two hundred and fifty five

Convert the decimal system to other systems

Decimal to octal

Here use: bc external command to complete. The bc command format is converted to: echo "obase= base; value" | bc

The code is as follows:

[chengmo@centos5 ~] $echo "obase=8;01234567" | bc

4553207

The same is true for converting binary, hexadecimal, and base64 to decimal.

The code is as follows:

[chengmo@centos5 ~] $echo "obase=64;123456" | bc

30 09 00

Shell, built-in a variety of binary representation is very simple. Just remember base#number. Remember to use the (()) symbol when assigning values. You can't just use the = sign. The = sign has no value type. The following is changed to a string by default. Such as:

The code is as follows:

[chengmo@centos5 ~] $num=0123

[chengmo@centos5 ~] $echo $num

0123

The beginning of 0 has lost its meaning.

The (()) operation effect can be achieved through the definer: let.

The code is as follows:

[chengmo@centos5 ~] $let num=0123

[chengmo@centos5 ~] $echo $num

eighty-three

Thank you for reading, the above is the "Shell script in the different binary data conversion method tutorial" content, after the study of this article, I believe you on the Shell script in different binary data conversion method tutorials have a deeper understanding of this problem, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report