bash - 在 Unix shell 中,如何以机器字节顺序将十六进制字符串转换为标准输出字节

标签 bash shell hex endianness xxd

我想运行类似于以下的命令:

# 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.

关于bash - 在 Unix shell 中,如何以机器字节顺序将十六进制字符串转换为标准输出字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66093712/

相关文章:

bash - 确定连接是有线还是无线?

linux - 如何删除 Bash 中的所有文件和子目录,包括边缘情况

arrays - 如何在数组中存储带有空格的元素?

java - 有没有Java版本管理器?

json - getent passwd 以正确格式输出为 json

c - 通过简单的函数调用将 32 位表示为 C 中的十六进制数

bash - 语法错误在bash脚本中使用expr substr

php - 如何解析 Linux shell 的长输出( "not text-processing friendly")?

java - 在十六进制文件中显示 0 值

java - 来自十六进制数的 NumberFormatException