linux shell脚本:十六进制字符串到字节 [英] linux shell scripting: hex string to bytes
本文介绍了linux shell脚本:十六进制字符串到字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个字符串 5a
。这是ASCII
字母Z的十六进制表示。我需要知道一个Linux shell命令,它将使用十六进制字符串
并输出字符串表示的二进制字节。
所以如果我做
echo 5a | command_im_looking_for> temp.txt
我打开 temp.txt
,我会看到一个孤立的字母Z。
解决方案
echo -n 5a | perl -pe's /([0-9a-f] {2})/ chr hex $ 1 / gie'
请注意,这不会跳过非十六进制字符。如果你只想要十六进制(没有原始字符串的空格等):
echo 5a | perl -ne / /([0-9a-f] {2})/ print chr hex $ 1 / gie'
另外, zsh
和 bash
本身支持 echo
echo -e'\x5a'
pre>
Lets say that I have a string
5a
. This is the hex representation of the ASCII letter Z. I need to know a Linux shell command which will take a hex string and output the binary bytes the string represents.So if I do
echo 5a | command_im_looking_for > temp.txt
and I open
temp.txt
, I will see a solitary letter Z.解决方案echo -n 5a | perl -pe 's/([0-9a-f]{2})/chr hex $1/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 $1/gie'
Also,
zsh
andbash
support this natively inecho
:echo -e '\x5a'
这篇关于linux shell脚本:十六进制字符串到字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文