简体   繁体   English

在bash脚本中将十进制转换为十六进制

[英]Converting decimal to hexadecimal in bash script

I want to convert a decimal number (loop index number) in a bash script to hexadecimal to be used by another command. 我想将bash脚本中的十进制数(循环索引号)转换为十六进制以供另一个命令使用。 Something like: 就像是:

for ((i=1; i<=100; i++))
do

     a=convert-to-decimal($i)
     echo "$a"

done

Where a should be hexadecimal with four digits and hex identifier. 其中a应为十六进制,带有四位数字和十六进制标识符。 For example if value of i is 100 , the value of a should be 0x0064 . 例如,如果i的值为100 ,则a的值应为0x0064 How to do it? 怎么做?

You can use printf . 你可以使用printf

$ printf "0x%04x" 100
0x0064

In your example you'd probably use a="$(printf '0x%04x' $i)" . 在你的例子中,你可能使用a="$(printf '0x%04x' $i)"

That's what you're looking for 这就是你要找的东西

for i in seq 1 100 ; 因为我在seq 1 100 ; do printf '%x\\n' $i done printf'%x \\ n'$ i完成了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM