Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux shell scripting: hex number to binary string

Tags:

linux

shell

hex

I am looking for some easy way in shell script for converting hex number into sequence of 0 and 1 characters.

Example:

5F -> "01011111"

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

like image 980
srnka Avatar asked Mar 07 '12 17:03

srnka


2 Answers

echo "ibase=16; obase=2; 5F" | bc
like image 163
Alex Howansky Avatar answered Sep 21 '22 00:09

Alex Howansky


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.

like image 28
Luchux Avatar answered Sep 21 '22 00:09

Luchux