Printing out a string as a byte array and corresponding hexadecimal
Submitted by admin on
Here is a quick script that is pretty handy!
#!/bin/bash
STR="Its a small world afterall"
CNT=$(wc -c <<< $STR})
TMP_CNT=0
STR="Its a small world afterall"
CNT=$(wc -c <<< $STR})
TMP_CNT=0
printf "Char Hex\n"
while [ ${TMP_CNT} -lt $[${CNT} -1] ]; do
printf "%-5s 0x%-2X\n" "${STR:$TMP_CNT:1}" "'${STR:$TMP_CNT:1}"
TMP_CNT=$[$TMP_CNT+1]
done
Which will output the following:
Char Hex
I 0x49
t 0x74
s 0x73
0x20
a 0x61
0x20
s 0x73
m 0x6D
a 0x61
l 0x6C
l 0x6C
0x20
w 0x77
o 0x6F
r 0x72
l 0x6C
d 0x64
0x20
a 0x61
f 0x66
t 0x74
e 0x65
r 0x72
a 0x61
l 0x6C
l 0x6C
0x0
I 0x49
t 0x74
s 0x73
0x20
a 0x61
0x20
s 0x73
m 0x6D
a 0x61
l 0x6C
l 0x6C
0x20
w 0x77
o 0x6F
r 0x72
l 0x6C
d 0x64
0x20
a 0x61
f 0x66
t 0x74
e 0x65
r 0x72
a 0x61
l 0x6C
l 0x6C
0x0
Add new comment