You can actually use printf
and sed
to split every 2 characters and output in whatever format you need. You use sed
to create a newline delimited list of 2-character strings and by outputting as a string you avoid the control character conversion issues with, e.g. 0D
, etc... You can use printf "0x%s\n"
(which by virtue of printf
processing each line of input will result in the output of all you 2-char values)
For example you could do:
printf "0x%s\n" $(echo "686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D" |
sed 's/\(..\)/\1\n/g')
or the equivalent with a herestring (bash only)
printf "0x%s\n" $(sed 's/\(..\)/\1\n/g' <<< "686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D")
Output
0x68
0x69
0x73
0x74
0x6F
0x72
0x79
0x20
0x0D
0x6C
0x73
0x20
0x2D
0x6C
0x74
0x72
0x20
0x0D
0x6E
0x20
0x6E
0x65
0x74
0x73
0x74
0x61
0x74
0x73
0x0D
0x6E
0x20
0x69
0x6E
0x66
0x6F
0x20
0x0D
0x6E
0x20
0x73
0x74
0x61
0x74
0x75
0x73
0x0D
0x65
0x78
0x69
0x74
0x0D
If you want the value in lower-case hex, then you can actually process the input values as hex using the "0x%02x"
conversion:
printf "0x%02x\n" $(echo "686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D" |
sed 's/\(..\)/0x\1\n/g')
Output
0x68
0x69
0x73
0x74
0x6f
0x72
0x79
0x20
0x0d
0x6c
0x73
0x20
0x2d
0x6c
0x74
0x72
0x20
0x0d
0x6e
0x20
0x6e
0x65
0x74
0x73
0x74
0x61
0x74
0x73
0x0d
0x6e
0x20
0x69
0x6e
0x66
0x6f
0x20
0x0d
0x6e
0x20
0x73
0x74
0x61
0x74
0x75
0x73
0x0d
0x65
0x78
0x69
0x74
0x0d
Which depending on what you want as output may be all you need. Let me know if you need a slightly different output format and I'm happy to help further.
Or -- If I have it backwards, and you actually want the ASCII value for every character in the string, you can use a simple for
loop and printf
to print the ASCII value of every character. To output the ASCII value, you precede the double-quoted variable name with a single-quote, e.g.
a="686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D"
for ((i = 0; i < ${#a}; i++)); do printf "%s - 0x%02x\n" "${a:i:1}" "'${a:i:1}"; done
Output
Character and ASCII value of each character:
6 - 0x36
8 - 0x38
6 - 0x36
9 - 0x39
7 - 0x37
3 - 0x33
7 - 0x37
4 - 0x34
6 - 0x36
F - 0x46
7 - 0x37
2 - 0x32
7 - 0x37
9 - 0x39
2 - 0x32
0 - 0x30
0 - 0x30
D - 0x44
6 - 0x36
C - 0x43
7 - 0x37
3 - 0x33
2 - 0x32
0 - 0x30
2 - 0x32
D - 0x44
6 - 0x36
C - 0x43
7 - 0x37
4 - 0x34
7 - 0x37
2 - 0x32
2 - 0x32
0 - 0x30
0 - 0x30
D - 0x44
6 - 0x36
E - 0x45
2 - 0x32
0 - 0x30
6 - 0x36
E - 0x45
6 - 0x36
5 - 0x35
7 - 0x37
4 - 0x34
7 - 0x37
3 - 0x33
7 - 0x37
4 - 0x34
6 - 0x36
1 - 0x31
7 - 0x37
4 - 0x34
7 - 0x37
3 - 0x33
0 - 0x30
D - 0x44
6 - 0x36
E - 0x45
2 - 0x32
0 - 0x30
6 - 0x36
9 - 0x39
6 - 0x36
E - 0x45
6 - 0x36
6 - 0x36
6 - 0x36
F - 0x46
2 - 0x32
0 - 0x30
0 - 0x30
D - 0x44
6 - 0x36
E - 0x45
2 - 0x32
0 - 0x30
7 - 0x37
3 - 0x33
7 - 0x37
4 - 0x34
6 - 0x36
1 - 0x31
7 - 0x37
4 - 0x34
7 - 0x37
5 - 0x35
7 - 0x37
3 - 0x33
0 - 0x30
D - 0x44
6 - 0x36
5 - 0x35
7 - 0x37
8 - 0x38
6 - 0x36
9 - 0x39
7 - 0x37
4 - 0x34
0 - 0x30
D - 0x44