Convert decimal to hexadecimal in bash script

I want to convert a decimal number (loop index number) in a bash script to a hex number that will be used by another command. Sort of:

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

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

done

Where a must be a hexadecimal with four digits and a hexadecimal identifier. For example, if the value of i is 100 , the value of a should be 0x0064 . How to do it?

+4
source share
2 answers

You can use printf.

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

In your example, you are probably using a="$(printf '0x%04x' $i)".

+2

seq 1 100;    printf '% x\n' $i

+1

Source: https://habr.com/ru/post/1628152/


All Articles