shell 二进制 十进制 十六进制 八进制转换

来源:互联网 发布:江阴网络诈骗判刑案例 编辑:程序博客网 时间:2024/05/18 02:43

c=$(echo "obase=2;$(($((2#$a))|$((2#$b))))" | bc)

十进制转八进制

这里使用到:bc外部命令完成。bc命令格式转换为:echo "obase=进制;值"|bc

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

二进制,十六进制,base64转换为 十进制也相同方法。

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

八进制转十进制:

[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

  1. $ a=1101; b=1000
  2. $ d=`echo $(($((2#$a))|$((2#$b)))) | bc`
  3.  echo "obase=2; $d" | bc        

0 0
原创粉丝点击