在 Unix shell 中,如何将十六进制字符串转换为机器字节序的标准输出字节
In Unix shell, how to convert from hex string to stdout bytes in machine-endian order
我想要运行一个类似的命令:
# 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 to 2
in normal mode, 4
in little-endian mode and 1
in bits mode. Grouping does not apply to postscript or include style.
我想要运行一个类似的命令:
# 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.