Hexadecimal to Binary conversion

Hi Guys,
Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out....
If I need to convert AF6D to binary...what could be the way to do?

Thanks in advance!!

---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ----------

I got

echo "ibase=16;obase=2;AF6D"|bc

workd for me..thanks alot all
cheers

echo "AF6D" | perl -ne 'printf "%016b\n", hex($_)'
1 Like
echo "ibase=16 ; obase=2 ; AF6D" | bc 
2 Likes

awk

echo "AF6D" | awk 'BEGIN {
	FS=""
	a["f"]="1111"
	a["e"]="1110"
	a["d"]="1101"
	a["c"]="1100"
	a["b"]="1011"
	a["a"]="1010"
	a["9"]="1001"
	a["8"]="1000"
	a["7"]="0111"
	a["6"]="0110"
	a["5"]="0101"
	a["4"]="0100"
	a["3"]="0011"
	a["2"]="0010"
	a["1"]="0001"
	a["0"]="0000"
	}
	{
	for (i=1;i<=NF;i++) printf a[tolower($i)]
	print ""
	}'
1010111101101101

Edit: other version found on unix.com

echo "AF6D" | awk --non-decimal-data ' {
for(i=lshift(1,15);i;i=rshift(i,1))
printf "%d%s", (and(("0x"$1)+0,i) > 0), ++c%4==0?" ":"";
printf"\n"; }'
1010 1111 0110 1101
1 Like

Remember!

Be aware......

echo "ibase=16 ; obase=2 ; AF6D" | bc

......strips leading zeros...

AF6D gives 16 bits; highest bit is a "1"...
7F6D gives 15 bits; leading "0" is stripped; highest bit is still "1"...

Maybe a something like this would help:-

typeset -Z16 binval=`echo "ibase=16 ; obase=2 ; 7F6D" | bc`
echo $binval

Just a thought.

Robin

Done in bash, should work in recent other shells:

VALUE=$((0xAF6D))
BITS=32
for ((i=BITS-1;i>=0;i--)); do printf "%d" $(( (VALUE>>i)%2 )); done; printf "\n"
00000000000000001010111101101101