在bash命令行中将十六进制字符串转换为ASCII [英] Conversion hex string into ascii in bash command line

查看:323
本文介绍了在bash命令行中将十六进制字符串转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这样的字符串,我想找到一个将其转换为ascii的命令,我尝试了echo -eod,但是没有用.

I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with echo -e and od, but it did not work.

0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2

推荐答案

此代码会将文本0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2转换为具有相等值的11个字节的流.这些字节将被写入标准输出.

This code will convert the text 0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2 into a stream of 11 bytes with equivalent values. These bytes will be written to standard out.

TESTDATA=$(echo '0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2' | tr '.' ' ')
for c in $TESTDATA; do
    echo $c | xxd -r
done

正如其他人指出的那样,由于指定的字节不是ASCII的简单原因,这将不会导致可打印的ASCII字符串.您需要发布有关如何获取此字符串的更多信息,以便我们为您提供帮助.

As others have pointed out, this will not result in a printable ASCII string for the simple reason that the specified bytes are not ASCII. You need post more information about how you obtained this string for us to help you with that.

工作原理:xxd -r将十六进制数据转换为二进制数据(如反向十六进制转储). xxd要求每行以该行第一个字符的索引号开始(对某些内容运行hexdump,并查看每行如何以索引号开始).在我们的例子中,我们希望该数字始终为零,因为每个执行只有一行.幸运的是,作为0x表示法的一部分,我们的数据在每个字符之前已经为零.小写的x被xxd忽略,因此我们要做的就是将每个0xhh字符传递给xxd并让它完成工作.

How it works: xxd -r translates hexadecimal data to binary (like a reverse hexdump). xxd requires that each line start off with the index number of the first character on the line (run hexdump on something and see how each line starts off with an index number). In our case we want that number to always be zero, since each execution only has one line. As luck would have it, our data already has zeros before every character as part of the 0x notation. The lower case x is ignored by xxd, so all we have to do is pipe each 0xhh character to xxd and let it do the work.

tr将句点转换为空格,以便for可以将其正确分割.

The tr translates periods to spaces so that for will split it up correctly.

这篇关于在bash命令行中将十六进制字符串转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆