How to print decimal value to hexadecimal values ​​in shell script

I have a requirement to have a string that has a decimal value. For example, '0 2930 0'. Now when I convert it to hex, it should say "0000 0B72 0000". I can convert the values ​​to hex using split and obase = 16 with bc, I will get the answer as "0 B72 0". I need to get the value "0000 0B72 0000". Can anyone help shed some light on this?

Here is what I did:

s1 ='2930'
echo after converting it to hexadecimal
s=`echo "obase=16; $s1" |bc`
echo $s
+4
source share
2 answers

Use printfto get output in the desired format:

$ printf "%04x " 0 2930 0
0000 0b72 0000
+5
source

Using bc like you

WANTED=456  ; echo "obase=16 ;$WANTED"|bc

and multiple

WANTED="{456 ;12 ;32000}"  ; echo "obase=16 ;$WANTED"|bc
+2
source

All Articles