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
Ascii/Hex convert in bash
提问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 Aa
should 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 hexdump
by default prints out 16-bit integers, not bytes. If your system has them, hd
(or hexdump -C
) or xxd
will provide less surprising outputs - if not, od -t x1
is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1c
to 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 -r
to convert back from hex (from the same format xxd
produces). If you just have plain hex (just the '4161', which is produced by xxd -p
) you can use xxd -r -p
to 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 0a
is the implicit newline that echo produces.
这0a
是 echo 产生的隐式换行符。
Use echo -n
or printf
instead.
使用echo -n
或printf
代替。
$ 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'
将削减任何可能的换行符在你的输入