shell16进制hex和10进制转换

方法:

1  bc命令

[root@centosgpt ~]# echo "ibase=16; FF" |bc
255

2.  bash

[root@centosgpt ~]# echo $((0xFF))
255

3.  printf

[root@centosgpt ~]# printf "%d\n" 0xFF
255

4. dc

[root@centosgpt ~]# dc -e '16i FF p'
255

限制:

其中bc,dc 可以不受整数位数限制  如:64-bit integer limit of 2^64, 当大于64位时验证下各个脚本的处理结果

1 bc

[root@centosgpt ~]# echo "ibase=16; 7FFFFFFFFFFFFFFFF" |bc
147573952589676412927

2 echo

[root@centosgpt ~]# echo $((0x7FFFFFFFFFFFFFFFF))
-1

3. printf

[root@centosgpt ~]# printf "%ld\n" 0x7FFFFFFFFFFFFFFFF
-bash: printf: warning: 0x7FFFFFFFFFFFFFFFF: Numerical result out of range
9223372036854775807

4. dc

[root@centosgpt ~]# dc -e '16i 7FFFFFFFFFFFFFFFF p'
147573952589676412927

参考:

hexadecimal-to-decimal-in-shell-script

Be First to Comment

发表评论

电子邮件地址不会被公开。 必填项已用*标注