十六进制到在Shell脚本十进制 [英] Hexadecimal To Decimal in Shell Script
问题描述
有人可以帮助我在讲述如何将十六进制数转换为十进制数在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 bash:
$ echo $((16#FF))
255
with bc:
$ echo "ibase=16; FF" | bc
255
with perl:
$ perl -le 'print hex("FF");'
255
with printf :
$ printf "%d\n" 0xFF
255
with python:
$ python -c 'print(int("FF", 16))'
255
with ruby:
$ ruby<<EOF
p "FF".to_i(16).to_s(10)
EOF
"255"
with node.js:
$ nodejs <<< "console.log(parseInt('FF', 16))"
255
with rhino:
$ rhino<<EOF
print(parseInt('FF', 16))
EOF
...
255
with groovy:
$ groovy -e 'println Integer.parseInt("FF",16)'
255
这篇关于十六进制到在Shell脚本十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!