如何在 Linux shell 中将十六进制转换为 ASCII 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1604765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:48:46  来源:igfitidea点击:

How to convert hex to ASCII characters in the Linux shell?

linuxbashshellencodingscripting

提问by Krystian Cybulski

Lets say that I have a string 5a.

假设我有一个 string 5a

This is the hex representation of the ASCII letter Z.

这是 ASCII 字母的十六进制表示Z

I need to know a Linux shell command which will take a hex string and output the ASCII characters that the string represents.

我需要知道一个 Linux shell 命令,它将采用一个十六进制字符串并输出该字符串代表的 ASCII 字符。

So if I do:

所以如果我这样做:

echo 5a | command_im_looking_for

I will see a solitary letter Z:

我会看到一封孤独的信Z

Z

采纳答案by bdonlan

echo -n 5a | perl -pe 's/([0-9a-f]{2})/chr hex /gie'

Note that this won't skip non-hex characters. If you want just the hex (no whitespace from the original string etc):

请注意,这不会跳过非十六进制字符。如果你只想要十六进制(原始字符串中没有空格等):

echo 5a | perl -ne 's/([0-9a-f]{2})/print chr hex /gie'

Also, zshand bashsupport this natively in echo:

此外,zshbash在本机支持这一点echo

echo -e '\x5a'

回答by unutbu

echo 5a | python -c "import sys; print chr(int(sys.stdin.read(),base=16))"

回答by ghostdog74

depending on where you got that "5a', you can just append \x to it and pass to printf

取决于你在哪里得到“5a”,你可以将 \x 附加到它并传递给 printf

$ a=5a
$ a="\x${a}"
$ printf "$a"
Z

回答by josch

I used to do this using xxd

我曾经使用 xxd 做到这一点

echo -n 5a | xxd -r -p

But then I realised that in Debian/Ubuntu, xxd is part of vim-common and hence might not be present in a minimal system. To also avoid perl (imho also not part of a minimal system) I ended up using sed, xargs and printf like this:

但后来我意识到在 Debian/Ubuntu 中,xxd 是 vim-common 的一部分,因此可能不会出现在最小系统中。为了避免 perl(恕我直言也不是最小系统的一部分),我最终使用了 sed、xargs 和 printf,如下所示:

echo -n 5a | sed 's/\([0-9A-F]\{2\}\)/\\\x/gI' | xargs printf

Mostly I only want to convert a few bytes and it's okay for such tasks. The advantage of this solution over the one of ghostdog74 is, that this can convert hex strings of arbitrary lengths automatically. xargs is used because printf doesnt read from standard input.

大多数情况下,我只想转换几个字节,这样的任务没问题。这种解决方案相对于 ghostdog74 的优势在于,它可以自动转换任意长度的十六进制字符串。使用 xargs 是因为 printf 不从标准输入读取。

回答by Fa11enAngel

You can make it through echo only and without the other stuff. Don't forget to add "-n" or you will get a linebreak automatically:

你可以只通过 echo 而没有其他东西。不要忘记添加“-n”,否则您将自动获得换行符:

echo -n -e "\x5a"

回答by Steven Penny

GNU awk 4.1

GNU awk 4.1

awk -niord '
$ echo 595a | awk -niord '
$ printf 595a | awk -niord '
$ printf a1 | awk -niord '
echo 5a | sha256sum | awk -bniord 'RT~/\w/,
#warning : spaces do matter
die(){ echo "$@" >&2;exit 1;}

p=48656c6c6f0a

test $((${#p} & 1)) == 0 || die "length is odd"
p2=''; for ((i=0; i<${#p}; i+=2));do p2=$p2\x${p:$i:2};done
printf "$p2"
=chr("0x"RT)' RS=.. ORS=
=chr("0x"RT)' RS=.. ORS= | od -tx1 0000000 c2 a1
=chr("0x"RT)' RS=.. ORS= | od -tx1c 0000000 59 5a Y Z
=chr("0x"RT)' RS=.. ORS= | od -tx1c 0000000 59 5a 00 Y Z
echo -n "5a" | while read -N2 code; do printf "\x$code"; done
=chr("0x"RT)' RS=.. ORS=

Note that if you echo to this it will produce an extra null byte

请注意,如果您对此进行回显,它将产生一个额外的空字节

$ echo 5a | (echo 16i; tr 'a-z' 'A-Z'; echo P) | dc
Z$

Instead use printf

而是使用 printf

$ echo 666f6f0a | python3 -c "import sys, binascii; sys.stdout.buffer.write(binascii.unhexlify(input().strip()))"
foo

Also note that GNU awk produces UTF-8 by default

另请注意,GNU awk 默认生成 UTF-8

$ echo foo | python3 -c "import sys, binascii; print(binascii.hexlify(sys.stdin.buffer.read()).decode())"
666f6f0a

If you are dealing with characters outside of ASCII, and you are going to be Base64 encoding the resultant string, you can disable UTF-8 with -b

如果您正在处理 ASCII 之外的字符,并且您打算对结果字符串进行 Base64 编码,则可以禁用 UTF-8 -b

##代码##

回答by Vouze

Here is a pure bash script (as printf is a bash builtin) :

这是一个纯 bash 脚本(因为 printf 是内置的 bash):

##代码##

If bash is already running, this should be faster than any other solution which is launching a new process.

如果 bash 已经在运行,这应该比启动新进程的任何其他解决方案都快。

回答by user3132194

Bash one-liner

Bash 单线

##代码##

回答by Roger Willcocks

dc can convert between numeric bases:

dc 可以在数字基数之间进行转换:

##代码##

回答by Hyman O'Connor

Some python3 one-liners that work with any number of bytes.

一些 python3 one-liners 可以处理任意数量的字节。

Decoding hex (with strip, so that it's ok to have a newline on stdin):

解码十六进制(使用strip,这样就可以在标准输入上换行了):

##代码##

Encoding hex:

编码十六进制:

##代码##