MOHAMED MOHAMED - 5 months ago 16
Linux Question

How to convert decimal to hex in bash?

I have a decimal number in the bash shell:

linux$ A=67


How do I print 67 as hexadecimal in bash?

Answer

As a bash program:

#!/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:

el@defiant ~ $ printf '%x\n' 26
1a
el@defiant ~ $ echo $((0xAA))
170
el@defiant ~ $