bash Shell脚本中的十六进制转十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13280131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hexadecimal To Decimal in Shell Script
提问by VenkateshJN
Can someone help me to convert a hexadecimal number to decimal number in a shell script?
有人可以帮我在 shell 脚本中将十六进制数转换为十进制数吗?
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.
例如,我想bfca3000
使用 shell 脚本将十六进制数转换为十进制数。我基本上想要两个十六进制数的差异。
My code is:
我的代码是:
var3=`echo "ibase=16; $var1" | bc`
var4=`echo "ibase=16; $var2" | bc`
var5=$(($var4-$var3)) # [Line 48]
When executing, I get this error:
执行时,我收到此错误:
Line 48: -: syntax error: operand expected (error token is "-")
回答by Gilles Quenot
To convert from hex to decimal, there are many ways to do it in the shell or with an external program:
要将十六进制转换为十进制,有很多方法可以在 shell 中或使用外部程序来完成:
With bash:
使用bash:
$ echo $((16#FF))
255
with bc:
与bc:
$ echo "ibase=16; FF" | bc
255
with perl:
使用perl:
$ perl -le 'print hex("FF");'
255
with printf:
与printf:
$ printf "%d\n" 0xFF
255
with python:
使用蟒蛇:
$ python -c 'print(int("FF", 16))'
255
with ruby:
与红宝石:
$ ruby -e 'p "FF".to_i(16)'
255
with node.js:
使用node.js:
$ nodejs <<< "console.log(parseInt('FF', 16))"
255
with rhino:
与犀牛:
$ rhino<<EOF
print(parseInt('FF', 16))
EOF
...
255
with groovy:
与groovy:
$ groovy -e 'println Integer.parseInt("FF",16)'
255
回答by hinekyle
Dealing with a very lightweight embedded version of busybox on Linux means many of the traditional commands are not available (bc, printf, dc, perl, python)
在 Linux 上处理一个非常轻量级的 busybox 嵌入式版本意味着许多传统命令不可用(bc、printf、dc、perl、python)
echo $((0x2f))
47
hexNum=2f
echo $((0x${hexNum}))
47
Credit to Peter Leungfor this solution.
此解决方案归功于Peter Leung。
回答by Tomás Fox
One more way to do it using the shell (bash or ksh, doesn't work with dash):
使用 shell 执行此操作的另一种方法(bash 或 ksh,不适用于破折号):
echo $((16#FF))
255
回答by ghoti
Various tools are available to you from within a shell. Sputnick has given you an excellent overview of your options, based on your initial question. He definitely deserves votes for the time he spent giving you multiple correct answers.
您可以在 shell 中使用各种工具。根据您最初的问题,Sputnick 为您提供了对您的选择的出色概述。他花时间给你多个正确答案,绝对值得投票。
One more that's not on his list:
还有一个不在他名单上的:
[ghoti@pc ~]$ dc -e '16i BFCA3000 p'
3217698816
But if all you want to do is subtract, why bother changing the input to base 10?
但是,如果您只想做减法,为什么还要将输入更改为以 10 为底的呢?
[ghoti@pc ~]$ dc -e '16i BFCA3000 17FF - p 10o p'
3217692673
BFCA1801
[ghoti@pc ~]$
The dc
command is "desk calc". It will also take input from stdin, like bc
, but instead of using "order of operations", it uses stacking ("reverse Polish") notation. You give it inputs which it adds to a stack, then give it operators that pop items off the stack, and push back on the results.
该dc
命令是“台计算”。它还将从标准输入获取输入,例如bc
,但它不使用“操作顺序”,而是使用堆叠(“反向波兰”)表示法。你给它输入,它添加到堆栈中,然后给它运算符从堆栈中弹出项目,然后推回结果。
In the commands above we've got the following:
在上面的命令中,我们有以下内容:
16i
-- tells dc to accept input in base 16 (hexadecimal). Doesn't change output base.BFCA3000
-- your initial number17FF
-- a random hex number I picked to subtract from your initial number-
-- take the two numbers we've pushed, and subtract the later one from the earlier one, then push the result back onto the stackp
-- print the last item on the stack. This doesn't change the stack, so...10o
-- tells dc to print its output in base "10", but remember that our input numbering scheme is currently hexadecimal, so "10" means "16".p
-- print the last item on the stack again ... this time in hex.
16i
-- 告诉 dc 接受基数为 16(十六进制)的输入。不改变输出基数。BFCA3000
-- 您的初始号码17FF
- 我选择从您的初始数字中减去的随机十六进制数字-
-- 取我们压入的两个数字,用前一个减去后一个,然后将结果压回到堆栈中p
-- 打印堆栈中的最后一项。这不会改变堆栈,所以......10o
-- 告诉 dc 以“10”为基数打印其输出,但请记住,我们的输入编号方案当前是十六进制的,因此“10”表示“16”。p
-- 再次打印堆栈上的最后一项……这次是十六进制。
You can construct fabulously complex math solutions with dc. It's a good thing to have in your toolbox for shell scripts.
您可以使用 dc 构建极其复杂的数学解决方案。在您的工具箱中拥有 shell 脚本是一件好事。
回答by ghoti
The error as reported appears when the variables are null (or empty):
当变量为空(或为空)时,会出现报告的错误:
$ unset var3 var4; var5=$(($var4-$var3))
bash: -: syntax error: operand expected (error token is "-")
That could happen because the value given to bc was incorrect. That might well be that bc needs UPPERcase values. It needs BFCA3000
, not bfca3000
. That is easily fixed in bash, just use the ^^
expansion:
这可能是因为给 bc 的值不正确。那很可能是 bc 需要大写值。它需要BFCA3000
,不是bfca3000
。这很容易在 bash 中修复,只需使用^^
扩展:
var3=bfca3000; var3=`echo "ibase=16; ${var1^^}" | bc`
That will change the script to this:
这会将脚本更改为:
#!/bin/bash
var1="bfca3000"
var2="efca3250"
var3="$(echo "ibase=16; ${var1^^}" | bc)"
var4="$(echo "ibase=16; ${var2^^}" | bc)"
var5="$(($var4-$var3))"
echo "Diference $var5"
But there is no need to use bc [1], as bash could perform the translation and substraction directly:
但是不需要使用 bc [1],因为 bash 可以直接进行翻译和减法:
#!/bin/bash
var1="bfca3000"
var2="efca3250"
var5="$(( 16#$var2 - 16#$var1 ))"
echo "Diference $var5"
[1]Note: I am assuming the values could be represented in 64 bit math, as the difference was calculated in bash in your original script. Bash is limited to integers less than ((2**63)-1) if compiled in 64 bits. That will be the only difference with bc which does not have such limit.
[1]注意:我假设这些值可以用 64 位数学表示,因为差异是在原始脚本中用 bash 计算的。如果以 64 位编译,Bash 仅限于小于 ((2**63)-1) 的整数。这将是与 bc 没有这种限制的唯一区别。
回答by novice
In dash and other shells, you can use
在破折号和其他外壳中,您可以使用
printf "%d\n" (your hexadecimal number)
to convert a hexadecimal number to decimal. This is not bash, or ksh, specific.
将十六进制数转换为十进制数。这不是特定于 bash 或 ksh 的。