我想运行类似于以下的命令:
# echo 00: 0123456789abcdef | xxd -r | od -tx1
0000000 01 23 45 67 89 ab cd ef
0000010
也就是说,我想输入一个十六进制字符串并将其转换为标准输出上的字节。但是,我希望它尊重我所在机器的字节顺序,即小端字节序。证据如下:
# lscpu | grep Byte.Order
Byte Order: Little Endian
所以,如果我的机器是大尾数法,我希望它能像上面那样工作。但既然不是,我想看看:
# <something different here> | od -tx1
0000000 ef cd ab 89 67 45 23 01
0000010
现在,xxd 有一个“-e”选项来实现小字节序。但是 1) 我想要机器字节序,因为我想要在大或小字节序机器上工作的东西,并且 2) “-e”无论如何都不支持“-r”。
谢谢!
最佳答案
这个怎么样——
$ echo 00: 0123456789abcdef | xxd -r | xxd -g 8 -e | xxd -r | od -tx1
0000000 ef cd ab 89 67 45 23 01
0000010
根据man xxd
:
-e
Switch to little-endian hexdump. This option treats byte groups as words in little-endian byte order. The default grouping of
4
bytes may be changed using-g
. This option only applies to hexdump, leaving the ASCII (or EBCDIC) representation unchanged. The command line switches-r
,-p
,-i
do not work with this mode.-g bytes | -groupsize bytes
Separate the output of every bytes bytes (two hex characters or eight bit-digits each) by a whitespace. Specify
-g 0
to suppress grouping. Bytes defaults to2
in normal mode,4
in little-endian mode and1
in bits mode. Grouping does not apply to postscript or include style.
关于bash - 在 Unix shell 中,如何以机器字节顺序将十六进制字符串转换为标准输出字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66093712/