Skip to content

Instantly share code, notes, and snippets.

@TimaGribanov
Created March 12, 2021 08:39
Show Gist options
  • Save TimaGribanov/28ccc38b204f93df17868ab6d43dcfac to your computer and use it in GitHub Desktop.
Save TimaGribanov/28ccc38b204f93df17868ab6d43dcfac to your computer and use it in GitHub Desktop.
Little bash script to convert HEX string into binary string
#!/bin/bash
INPUTVAR="$1"
INPUTVARSIZE=${#INPUTVAR}
LOOPCOUNTER=0
while [ $LOOPCOUNTER -lt $INPUTVARSIZE ]
do
HEXVAR="${INPUTVAR:$LOOPCOUNTER:1}"
case $HEXVAR in
0)
BINVAR="0000"
;;
1)
BINVAR="0001"
;;
2)
BINVAR="0010"
;;
3)
BINVAR="0011"
;;
4)
BINVAR="0100"
;;
5)
BINVAR="0101"
;;
6)
BINVAR="0110"
;;
7)
BINVAR="0111"
;;
8)
BINVAR="1000"
;;
9)
BINVAR="1001"
;;
A)
BINVAR="1010"
;;
B)
BINVAR="1011"
;;
C)
BINVAR="1100"
;;
D)
BINVAR="1101"
;;
E)
BINVAR="1110"
;;
F)
BINVAR="1111"
;;
esac
BINTEXT+=$BINVAR
((LOOPCOUNTER++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment