Linux 如何在bash中将十进制转换为十六进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16527113/
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-08-06 22:55:35 来源:igfitidea点击:
How to convert decimal to hex in bash?
提问by MOHAMED
I have a decimal number in the bash shell:
我在 bash shell 中有一个十进制数:
linux$ A=67
How do I print 67 as hexadecimal in bash?
如何在 bash 中将 67 打印为十六进制?
采纳答案by Vorsprung
As a bash program:
作为 bash 程序:
#!/bin/bash
decimal1=31
printf -v result1 "%x" "$decimal1"
decimal2=33
printf -v result2 "%x" "$decimal2"
echo $result1 $decimal1
echo $result2 $decimal2
Or directly from the bash shell:
或者直接从 bash shell:
el@defiant ~ $ printf '%x\n' 26
1a
el@defiant ~ $ echo $((0xAA))
170
el@defiant ~ $