bash 在bash命令行中将十六进制字符串转换为ascii

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13160309/
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-09-09 22:53:59  来源:igfitidea点击:

Conversion hex string into ascii in bash command line

bashhex

提问by Kerby82

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 -eand od, but it did not work.

我有很多这种字符串,我想找到一个命令将它转换为 ascii,我尝试使用echo -eand od,但它没有用。

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

采纳答案by Dan Bliss

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

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

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

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.

正如其他人指出的那样,这不会产生可打印的 ASCII 字符串,原因很简单,即指定的字节不是 ASCII。您需要发布有关您如何获得此字符串的更多信息,以便我们帮助您。

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.

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

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

tr 将句点转换为空格,以便 for 将其正确拆分。

回答by funroll

This worked for me.

这对我有用。

$ echo 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$

-rtells it to convert hex to ascii as opposed to its normal mode of doing the opposite

-r告诉它将十六进制转换为 ascii,而不是它的正常模式进行相反的操作

-ptells it to use a plain format.

-p告诉它使用普通格式。

回答by Raghu Sodha

You can use something like this.

你可以使用这样的东西。

$ cat test_file.txt
54 68 69 73 20 69 73 20 74 65 78 74 20 64 61 74 61 2e 0a 4f 6e 65 20 6d 6f 72 65 20 6c 69 6e 65 20 6f 66 20 74 65 73 74 20 64 61 74 61 2e

$ for c in `cat test_file.txt`; do printf "\x$c"; done;
This is text data.
One more line of test data.

回答by Yokai

The values you provided are UTF-8 values. When set, the array of:

您提供的值为 UTF-8 值。设置后,数组:

declare -a ARR=(0xA7 0x9B 0x46 0x8D 0x1E 0x52 0xA7 0x9B 0x7B 0x31 0xD2)

Will be parsed to print the plaintext characters of each value.

将被解析以打印每个值的纯文本字符。

for ((n=0; n < ${#ARR[*]}; n++)); do echo -e "\u${ARR[$n]//0x/}"; done

And the output will yield a few printable characters and some non-printable characters as shown here:

输出将产生一些可打印的字符和一些不可打印的字符,如下所示:

enter image description here

在此处输入图片说明

For converting hex values to plaintext using the echocommand:

使用以下echo命令将十六进制值转换为纯文本:

echo -e "\x<hex value here>"

And for converting UTF-8 values to plaintext using the echocommand:

并使用以下echo命令将 UTF-8 值转换为纯文本:

echo -e "\u<UTF-8 value here>"

And then for converting octal to plaintext using the echocommand:

然后使用以下echo命令将八进制转换为纯文本:

echo -e "
echo -e $(your_program with arguments | sed -e 's/0x\(..\)\.\?/\x/g')
<octal value here>"

When you have encoding values you aren't familiar with, take the time to check out the ranges in the common encoding schemes to determine what encoding a value belongs to. Then conversion from there is a snap.

如果您有不熟悉的编码值,请花点时间查看常用编码方案中的范围,以确定值属于哪种编码。然后从那里转换是一个单元。

回答by Alexander Amelkin

The echo -emust have been failing for you because of wrong escaping. The following code works fine for me on a similar output from your_program with arguments:

echo -e必须已经失败,因为错的逸出的为您服务。以下代码对我的类似输出工作正常your_program with arguments

#!/bin/bash

echo $((0x)).$((0x)).$((0x)).$((0x))

Please note however that your original hexstring consists of non-printable characters.

但是请注意,您的原始十六进制字符串由不可打印的字符组成。

回答by Cleber Nunes

Make a script like this:

制作一个这样的脚本:

$cat hex.txt
68 65 6c 6c 6f
$cat hex.txt | xxd -r -p
hello

Example:

例子:

sh converthextoip.sh c0 a8 00 0b

sh converthextoip.sh c0 a8 00 0b

Result:

结果:

192.168.0.11

192.168.0.11

回答by user2538235

You can use xxd:

您可以使用xxd

##代码##