Shell programming: conversion and calculation between strings and numbers

Original: http://www.cnblogs.com/chengmo/archive/2010/10/14/1851570.html



The shell can represent different binary data without invoking third-party commands. The following representation methods are summarized here. The default value of the shell script is to deal with the decimal number, unless the number starts with a special notation or prefix. It can represent other base type values. For example: starting with 0 is octal. Starting with 0x is hexadecimal. Use BASE#NUMBER to represent other bases. BASE value: 2-64.

 

Instructions:

  • Convert other bases to decimal

Octal to decimal:

[chengmo@cents5 ~]$ ((num=0123));
[chengmo@centos5 ~]$ echo $num;
83

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

((expression)), (()) can be any data expression. If you add: "$" in front, you can read the calculation result.

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

base64 to decimal:

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

binary to decimal

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

 

  • Convert decimal to other bases

Decimal to Octal

Here is used: bc external command is completed. The format of the bc command is converted to: echo "obase=hexadecimal; value"|bc

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

Convert binary, hexadecimal, base64 to decimal in the same way.

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

 

Shell, built-in various binary representation methods are very simple. Just remember base#number. Remember to use the (()) symbol when assigning values. You can't use the = sign directly. The = sign has no value type. By default, the back will be turned into a string . Such as:

[chengmo@centos5 ~]$ num=0123;
[chengmo@centos5 ~]$ echo $num;
0123

0 at the beginning has lost its meaning.

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

[chengmo@centos5 ~]$ let num=0123;
[chengmo@centos5 ~]$ echo $num;  
83

 

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



=================================

Shell programming: conversion and calculation between strings and numbers

================================

Original: http://blog.csdn.net/qianlong4526888/article/details/8516461

Shell programming often needs to operate on strings, and sometimes it is necessary to convert strings into numerical values ​​and perform addition and subtraction operations. The following describes how to convert strings to numeric values ​​and perform calculations.

# temp1=400d7c

#  echo $((16#${temp1}+4)) (The default print is decimal output)

4197760

# temp2 = $ ((16# $ {temp1} +4))

# echo "obase=16;${temp2}"|bc

400D80

The above content is to convert the string to hexadecimal, perform operation, and then print it out in hexadecimal.



===================================== Gorgeous summary line =========== ============================

Assignment and printing in the shell are in decimal by default. Unless you indicate what base it is. Converting a string to a number is actually using the operator (()). The red part above is very clear. (()) has the same effect as let.
Example I gave:

Example 1: Effect without (())
[huntinux@huntinux 6chp]$ foo=0x400d
[huntinux@huntinux 6chp]$ echo $foo
0x400d
[huntinux@huntinux 6chp]$ ((foo=0x400d ))
[huntinux@huntinux 6chp]$ echo $foo
16397 The

above 6 lines indicate that if (()) is not used, then the default foo is a string. But if (()) is added, it is considered to be a number, and the default is decimal when outputting.

Example 2: What should I do if I forget to add (())? ---> Make up once (())
[huntinux@huntinux 6chp]$ foo=0x400d
[huntinux@huntinux 6chp]$ echo $foo
0x400d
[huntinux@huntinux 6chp]$ ((foo=$foo))
[huntinux@huntinux 6chp]$ echo $foo
16397

However, it seems that $(()) sticks to these details.
[huntinux@huntinux 6chp]$ foo=0x400d
[huntinux@huntinux 6chp]$ echo $((foo+1))
16398

Is it possible to replace foo with 0x400d first? Then do the calculation?
That is: $((foo+1)) --> $((0x400d+1))-->Calculate.
Here I leave myself a question. Hope informed netizens give pointers.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370217&siteId=291194637