我正在尝试将十六进制数字转换为 ASCII 文本,为此我使用了xxd
. 但问题是它没有抛出任何输出。
例如,我使用:
echo 86973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D | xxd -r -p
但它没有给我任何输出。
没有在线生成器的帮助,还有其他方法可以隐藏这个吗?请帮忙!!
您的十六进制数据有许多 CR ( 0D
) 字节,这会导致光标倒回到行首并覆盖任何先前的输出。此外,当管道完成运行时,您的 shell 提示符将覆盖任何剩余的输出。
您很可能希望通过附加| tr '\r' '\n'
到管道将 CR 转换为 LF 以查看输出:
$ echo 686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D | xxd -r -p | tr '\r' '\n'
history
ls -ltr
n netstats
n info
n status
exit
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
您可以使用 python3 inline 打印 ASCII 字符。这可能不像其他答案那样模仿 xxd 输出,但允许您查看导致问题的 \r\n 类型的字符。
$ str='686973746F7279200D6C73202D6C7472200D6E206E657473746174730D6E20696E666F200D6E207374617475730D657869740D'
$ python3 -c "b=b''.fromhex('$str'); s=str(b)[2:-1]; print(s)"
history \rls -ltr \rn netstats\rn info \rn status\rexit\r
这里,
b=b''.fromhex('$str')
将十六进制数字转换为字节对象。如果你使用过 python,对于像换行符这样的字符 str(b)[2:-1]
将看起来像的字节对象转换b'abcd'
为abcd
. 和s 对输出并不重要,因此被截断b
。'
\r
@rtx13 的答案中的 as,请添加s.replace('\r', '=n')
.发送到 xxd 时,您的输出中可能会出现额外的换行符。echo 在输出中添加一个换行符。展示:
$ echo '' | xxd
00000000: 0a
当您不想在管道中添加字节时,请使用printf
更一致的行为。