十六进制到在Shell脚本十进制 [英] Hexadecimal To Decimal in Shell Script

查看:264
本文介绍了十六进制到在Shell脚本十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我在讲述如何将十六​​进制数转换为十进制数在shell脚本?例如。我要转换的十六进制数 bfca3000 使用shell脚本为十进制。我基本上要两个十六进制数之差。我的code是:

  VAR3 =`回声广积= 16; $ VAR1| bc`
VAR4 =`回声广积= 16; $ VAR2| bc`
VAR5 = $(($ var4- $ VAR3))#[线路48]

执行后,我得到的错误为:

  48号线 - :语法错误:预期操作数(误差令牌 - )


解决方案

要转换HEX2DEC,这些都是很多方法可以做到这一点在shell或脚本:

使用庆典

  $回声$((16#FF))
255

使用 BC 问题:

  $回声广积= 16; FF|公元前
255

使用 perl的

  $的perl -le'打印十六进制(FF);
255

使用的printf

  $ printf的%d个\\ N为0xFF
255

使用蟒蛇

  $蟒蛇-c'打印(INT(FF,16))'
255

使用红宝石

  $红宝石<< EOF
PFF.to_i(16).to_s(10)
EOF
255

使用的node.js

  $&的NodeJS LT;<< 的console.log(parseInt函数('FF',16))
255

使用犀牛

  $犀牛<< EOF
打印(parseInt函数('FF',16))
EOF
...
255

使用常规

  $常规-e'调用println的Integer.parseInt(FF,16)
255

Can someone help me in telling how to convert a hexadecimal number to decimal number in a shell script? E.g. I want to convert the hexadecimal number bfca3000 to decimal using a shell script. I basically want the difference of two hexadecimal numbers. My code is:

var3=`echo "ibase=16; $var1" | bc`
var4=`echo "ibase=16; $var2" | bc`
var5=$(($var4-$var3))               # [Line 48]

Upon executing ,I am getting the error as:

Line 48: -: syntax error: operand expected (error token is "-")

解决方案

To convert hex2dec, these are many ways to do it in a shell or a script :

With :

$ echo $((16#FF))
255

with :

$ echo "ibase=16; FF" | bc
255

with :

$ perl -le 'print hex("FF");'
255

with :

$ printf "%d\n" 0xFF
255

with :

$ python -c 'print(int("FF", 16))'
255

with :

$ ruby<<EOF
p "FF".to_i(16).to_s(10)
EOF
"255"

with :

$ nodejs <<< "console.log(parseInt('FF', 16))"
255

with :

$ rhino<<EOF
print(parseInt('FF', 16))
EOF
...
255

with :

$ groovy -e 'println Integer.parseInt("FF",16)'
255

这篇关于十六进制到在Shell脚本十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆