bash 中的 Ascii/Hex 转换

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

Ascii/Hex convert in bash

bashasciihex

提问by gdb

I'm now doing it this way:

我现在是这样做的:

[root@~]# echo Aa|hexdump -v
0000000 6141 000a                              
0000003
[root@~]# echo -e "\x41\x41\x41\x41"
AAAA

But it's not exactly behaving as I wanted,

但它并不完全按照我的意愿行事,

the hex form of Aashould be 4161,but the output is 6141 000a,which seems not making sense.

的十六进制形式Aa应该是4161,但输出是6141 000a,这似乎没有意义。

and when performing hex to ascii,is there another utility so that I don't need the prefix \x?

当执行十六进制到 ascii 时,是否有另一个实用程序以便我不需要前缀\x

回答by Random832

The reason is because hexdumpby default prints out 16-bit integers, not bytes. If your system has them, hd(or hexdump -C) or xxdwill provide less surprising outputs - if not, od -t x1is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1cto show both the byte hex values and the corresponding letters.

原因是hexdump默认打印出 16 位整数,而不是字节。如果您的系统有它们,hd(或hexdump -C)或xxd将提供不那么令人惊讶的输出 - 如果没有,od -t x1则是获取逐字节十六进制输出的 POSIX 标准方式。您可以使用od -t x1c来显示字节十六进制值和相应的字母。

If you have xxd(which ships with vim), you can use xxd -rto convert back from hex (from the same format xxdproduces). If you just have plain hex (just the '4161', which is produced by xxd -p) you can use xxd -r -pto convert back.

如果你有xxd(它与 vim 一起提供),你可以使用xxd -r从十六进制转换回来(从相同的格式xxd产生)。如果您只有普通的十六进制(只有'4161',由 生成xxd -p),您可以使用它xxd -r -p来转换回来。

回答by Mikel

For the first part, try

对于第一部分,尝试

echo Aa | od -t x1

It prints byte-by-byte

它逐字节打印

$ echo Aa | od -t x1
0000000 41 61 0a
0000003

The 0ais the implicit newline that echo produces.

0a是 echo 产生的隐式换行符。

Use echo -nor printfinstead.

使用echo -nprintf代替。

$ printf Aa | od -t x1
0000000 41 61
0000002

回答by bash-o-logist

$> printf "%x%x\n" "'A" "'a"
4161

回答by F?rat Kü?üK

For single line solution:

对于单线解决方案:

echo "Hello World" | xxd -ps -c 200 | tr -d '\n'

It will print:

它会打印:

48656c6c6f20576f726c640a

or for files:

或文件:

cat /path/to/file | xxd -ps -c 200 | tr -d '\n'

For reverse operation:

对于反向操作:

echo '48656c6c6f20576f726c640a' | xxd -ps -r

It will print:

它会打印:

Hello World

回答by Vouze

With bash :

使用 bash :

a=abcdefghij    
for ((i=0;i<${#a};i++));do printf %02X \'${a:$i:1};done

6162636465666768696A

6162636465666768696A

回答by Kundan Kumar

I don't know how it crazy it looks but it does the job really well

我不知道它看起来有多疯狂,但它确实做得很好

ascii2hex(){ a="$@";s=0000000;printf "$a" | hexdump | grep "^$s"| sed s/' '//g| sed s/^$s//;}

Created this when I was trying to see my name in HEX ;) use how can you use it :)

当我试图在十六进制中看到我的名字时创建了这个 ;) 使用你如何使用它 :)

回答by blogresponder

here a little script I wrote to convert ascii to hex. hope it helps:

这是我写的一个小脚本,用于将 ascii 转换为十六进制。希望能帮助到你:

echo '0x'"`echo 'ASCII INPUT GOES HERE' | hexdump -vC |  awk 'BEGIN {IFS="\t"} {=""; print }' | awk '{sub(/\|.*/,"")}1'  | tr -d '\n' | tr -d ' '`" | rev | cut -c 3- | rev

回答by SteinAir

Text2Conv="Aa"
for letter in $(echo "$Text2Conv" | sed "s/\(.\)/' /g");do printf '%x' "$letter";done

4161

4161

The trick is using sed to parse the Text2Conv to format we can then seperate anf loop using for.

诀窍是使用 sed 解析 Text2Conv 以格式化我们然后可以使用 for 分离 anf 循环。

回答by scoombs

SteinAir's answer above was helpful to me -- thank you! And below is a way it inspired, to convert hex strings to ascii:

SteinAir 上面的回答对我很有帮助——谢谢!下面是它启发的一种将十六进制字符串转换为 ascii 的方法:

for h in $(echo "4161" | sed "s/\(..\)/ /g"); do printf `echo "\x$h"`;done
Aa

回答by stelios

I use:

我用:

> echo Aa | tr -d '\n' | xxd -p
4161

> echo 414161 | tr -d '\n' | xxd -r -p
AAa

The tr -d '\n'will trim any possible newlines in your input

tr -d '\n'将削减任何可能的换行符在你的输入