[chengmo@centos5 ~]$ ((num=0123));
[chengmo@centos5 ~]$ echo $num;
83
[chengmo@centos5 ~]$ ((num=8#123));
[chengmo@centos5 ~]$ echo $num;
83
((表达式)),(())里面可以是任意数据表达式。如果前面加入:”$”可以读取计算结果。
十六进制转十进制:
[chengmo@centos5 ~]$ ((num=0xff));
[chengmo@centos5 ~]$ echo $num;
255
[chengmo@centos5 ~]$ ((num=16#ff));
[chengmo@centos5 ~]$ echo $num;
255
base-32转十进制:
[chengmo@centos5 ~]$ ((num=32#ffff));
[chengmo@centos5 ~]$ echo $num;
507375
base64转十进制:
[chengmo@centos5 ~]$ ((num=64#abc_));
[chengmo@centos5 ~]$ echo $num;
2667327
二进制转十进制
[chengmo@centos5 ~]$ ((num=2#11111111));
[chengmo@centos5 ~]$ echo $num;
255
十进制转为其它进制
十进制转八进制
这里使用到:bc外部命令完成。bc命令格式转换为:echo "obase=进制;值"|bc
[chengmo@centos5 ~]$ echo "obase=8;01234567"|bc
4553207
二进制,十六进制,base64转换为 十进制也相同方法。
[chengmo@centos5 ~]$ echo "obase=64;123456"|bc
30 09 00
shell,内置各种进制表示方法非常简单。记得base#number 即可。这里记得赋值时候用(())符号。不能直接用=号了。=号没有值类型。默认将后面变成字符串了。如:
[chengmo@centos5 ~]$ num=0123;
[chengmo@centos5 ~]$ echo $num;
0123
0开头已经失去了意义了。
可以通过定义符:let达到(()) 运算效果。
[chengmo@centos5 ~]$ let num=0123;
[chengmo@centos5 ~]$ echo $num;
83
本文详细介绍了如何在Linux shell中将八进制数转换为十进制数,包括使用内置表达式和外部命令bc进行转换的方法,并提供了实际示例和注意事项。
1911

被折叠的 条评论
为什么被折叠?



