How I can convert in bash a string in hex (for example "d43c23F1") to binary.
Without using the utility bc.
Hex:Binary
d43c23F1:11010100001111000010001111110001
Every hex digit corresponds to exactly four binary digits. Make a dictionary of those mappings and loop over every hex character, appending the four digits to the answer. I'm not posting code since I haven't that much time, but you should be able to proceed from here.
Can you use dc
instead? Then you can do:
$ echo 16 i 2 o D43C23F1 p | dc
11010100001111000010001111110001
Otherwise, you could follow Emil's suggestion using bash "associative arrays". There is a Linux Journal article that talks about them in some detail. Create a dictionary that maps hex digits to their binary representation:
declare -A bits
bits[A]=1010
Split your hex string into digits, then look each one up in the array.
Shamelessly, stolen from here, with modifications for hex.
#!/bin/bash
# SCRIPT: hex2binary.sh
# USAGE: hex2binary.sh Hex_Number(s)
# PURPOSE: Hex to Binary Conversion. Takes input as command line
# arguments.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################
if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: hex2binary.sh hex_number(s)"
else
echo -e "\033[1mHEX \t\t BINARY\033[0m"
while [ $# -ne 0 ]
do
DecNum=`printf "%d" $1`
Binary=
Number=$DecNum
while [ $DecNum -ne 0 ]
do
Bit=$(expr $DecNum % 2)
Binary=$Bit$Binary
DecNum=$(expr $DecNum / 2)
done
echo -e "$Number \t\t $Binary"
shift
# Shifts command line arguments one step.Now $1 holds second argument
unset Binary
done
fi
You must start your hex number with 0x
for this to work.
this code assumes your hex is in caps (necessary for other bases that may use capital and lower case) - just add the functions to your bashrc
toupper() { #change input $@ to upper case
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $lowers in
*$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
printf "${OUTPUT}"
}
baseN2dec(){ #usage: baseN2dec number <base> (default base is 16)
#note base32 and base64 should really have a different string pattern ... todo
#RFC 4648 Base32 alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 we use base32hex
#Base 64 is ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
STRING=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
i=$((${#1}-1));DEC=0;POS=1;BASE=${2:-16}
while ([ $i -ge 0 ]) do
VAL=${1:$i:1}
VAL=${STRING%${VAL}*}
VAL=${#VAL}
DEC=$(($VAL*$POS+$DEC))
POS=$(($BASE*$POS))
i=$(($i-1))
done
echo $DEC
}
dec2baseN(){ #usage: dec2baseN number <base>
A=$(($1/$2))
STRING=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
[ $A -ge $2 ] && dec2baseN ${A} $2 || printf ${STRING:$A:1}
printf ${STRING:$(($1%$2)):1}
}
dec2bin(){
dec2baseN $1 2
}
dec2hex(){
dec2baseN $1 16
}