bash 如何在shell中将十六进制数与十六进制数进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13500866/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:50:23  来源:igfitidea点击:

How to compare hexadecimal numbers with hexadecimal numbers in shell?

bashshell

提问by bd1257

How to compare hexadecimal number with hexadecimal numbers in shell?

如何将十六进制数与shell中的十六进制数进行比较?

采纳答案by thkala

At least bashsupports hexadecimal integers directly, provided that they are prefixed with 0x:

至少bash直接支持十六进制整数,前提是它们带有前缀0x

$ [[ 0xdead -lt 0xcafe ]] && echo yes || echo no
no
$ [[ 0xdead -gt 0xcafe ]] && echo yes || echo no
yes

You just use the comparison operators normally...

您通常只需使用比较运算符...

回答by neoben

You can convert your hex numbers into decimals using printfand then you can compare the numeric values, e.g.:

您可以使用将十六进制数字转换为小数printf,然后您可以比较数值,例如:

x="0xdead"
y="0xcafe"

x_num=$(printf "%d" "$x")
y_num=$(printf "%d" "$y")

if [ $x_num -gt $y_num ]; then
    echo "x is my value"
else
   echo "x is not my value"
fi

回答by cnicutar

How about

怎么样

(( "$answer" == 0x42 ))
echo $?

answer=0xDEADCAFE
(( "$answer" == 0xDEADCAFE ))
echo $?