I have a requirement of having a string which has a decimal value. For e.g., '0 2930 0'. Now , when i convert it to hex, it should say '0000 0B72 0000'. i'm able to convert the values to hex using split and obase=16 with bc, i'll get the answer as '0 B72 0'. What i needed is to get the value '0000 0B72 0000'. Can anyone help shed some light on this?
Here is what i've done:
s1 ='2930'
echo after converting it to hexadecimal
s=`echo "obase=16; $s1" |bc`
echo $s
Use printf
for getting the output in the desired format:
$ printf "%04x " 0 2930 0
0000 0b72 0000
Using bc as you have,
WANTED=456 ; echo "obase=16 ;$WANTED"|bc
and multiples
WANTED="{456 ;12 ;32000}" ; echo "obase=16 ;$WANTED"|bc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With