2

shell中如何将其他进制数转换成10进制数

 2 years ago
source link: https://www.lujun9972.win/blog/2018/12/18/shell%E4%B8%AD%E5%A6%82%E4%BD%95%E5%B0%86%E5%85%B6%E4%BB%96%E8%BF%9B%E5%88%B6%E6%95%B0%E8%BD%AC%E6%8D%A2%E6%88%9010%E8%BF%9B%E5%88%B6%E6%95%B0/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

shell中如何将其他进制数转换成10进制数

通过给bc设置 obase(输出的基数)ibase(输入的基数),可以进行任何进制的转换:

echo "obase=10;ibase=16;$num" |bc

通过基数表示法转换

在shell中,以 0 为前导的常量被当作八进制数,以 0x 或 0X 作为前导表明是十六进制。 其他情况下,数字的形式是 [base#]n ,这里 base 是一个 2 到 64 的十进制数值,作为数字的基数,n 是在这个基数中数字的值。

echo $((16#$num))
echo $((0X$num))

12 12


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK