Printing out a string as a byte array and corresponding hexadecimal

Here is a quick script that is pretty handy!

#!/bin/bash
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

Blog tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <python> <c>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.