How to read string as hex number in bash?
Probably a better way to do this but I've come up with this solution which converts the number to decimal and then back to hex (and manually adds the 0x
):
printf '0x%x\n' "$((16#00080000))"
Which you could write as:
printf '0x%x\n' "$((16#$(expr substr "$SUPERBLOCK" 64 8)))"
There are two more or less standard (and ancient) command-line unix tools that offer very easy ways to convert numbers between different bases:
$ { echo '16'; echo i; echo 00080000; echo p; } | dc
524288
$ { echo 'ibase=16'; echo 00080000; } | bc
524288
For normal human use I very much prefer bc
, but when writing a program that generates code, especially from a parser of some sort, a stack-based tool like dc
may be easier to deal with (and indeed the original version of bc
was a front-end parser for dc
).