Linux shell scripting: hex number to binary string

echo "ibase=16; obase=2; 5F" | bc

I used 'bc' command in Linux. (much more complex calculator than converting!)

echo 'ibase=16;obase=2;5f' | bc

ibase parameter is the input base (hexa in this case), and obase the output base (binary).

Hope it helps.


$ printf '\x5F' | xxd -b | cut -d' ' -f2
01011111

Or

$ dc -e '16i2o5Fp'
1011111
  • The i command will pop the top of the stack and use it for the input base.
  • Hex digits must be in upper case to avoid collisions with dc commands and are not limited to A-F if the input radix is larger than 16.
  • The o command does the same for the output base.
  • The p command will print the top of the stack with a newline after it.

Tags:

Linux

Hex

Shell