简体   繁体   中英

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. 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. For example if value of i is 100 , the value of a should be 0x0064 . How to do it?

You can use printf .

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

In your example you'd probably use a="$(printf '0x%04x' $i)" .

That's what you're looking for

for i in seq 1 100 ; do printf '%x\\n' $i done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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