Search code examples
linuxshellhex

Linux shell scripting: hex number to binary string


I am looking for an easy way in a shell script to convert a hex number into a sequence of 0 and 1 characters.

Example:

5F -> "01011111"

Is there any command or easy method to accomplish this or should I write some switch for it?


Solution

  • 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.