在Unix Shell中,如何通过机器 - ordian顺序从十六进制字符串转换为stdout字节

发布于 2025-02-10 06:49:25 字数 560 浏览 1 评论 0原文

我想运行类似的命令:

# echo 00: 0123456789abcdef | xxd -r | od -tx1
0000000 01 23 45 67 89 ab cd ef
0000010

也就是说,我想输入一个十六进制字符串,并将其转换为stdout上的字节。但是,我希望它尊重我正在使用的机器的字节订单,这是小末日。这是证据:

# lscpu | grep Byte.Order
Byte Order:            Little Endian

所以,如果我的机器是大型的,我希望它能像上面一样工作。但是由于不是,所以我想看到:

# <something different here> | od -tx1
0000000 ef cd ab 89 67 45 23 01
0000010

现在,XXD对于Little Endianess具有“ -e”选项。但是1)我想要机器端,因为我想要在大型或小的机器上使用的东西,而2)“ -e”无论如何都不支持“ -r”。

谢谢!

I'd like to run a command similar to:

# echo 00: 0123456789abcdef | xxd -r | od -tx1
0000000 01 23 45 67 89 ab cd ef
0000010

That is, I'd like to input a hex string and have it converted to bytes on stdout. However, I'd like it to respect byte order of the machine I'm on, which is little endian. Here's the proof:

# lscpu | grep Byte.Order
Byte Order:            Little Endian

So, I'd like it to work as above if my machine was big-endian. But since it isn't, I'd like to see:

# <something different here> | od -tx1
0000000 ef cd ab 89 67 45 23 01
0000010

Now, xxd has a "-e" option for little endianess. But 1) I want machine endianess, because I'd like something that works on big or little endian machines, and 2) "-e" isn't support with "-r" anyway.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-02-17 06:49:25

那呢 -

$ 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

    切换到 Little-endian hexdump。此选项将字节组视为小型字节顺序中的单词。 4字节的默认分组可以使用-G更改。此选项仅适用于Hexdump,使ASCII(或EBCDIC)表示不变。命令行开关-r-p-i不使用此模式。

  • -g bytes | -groupsize bytes

    将每个字节的输出 bytes(两个十六进制字符或八个位数)与空格分开。指定-g 0以抑制分组。 bytes 在正常模式下默认为24在小丹模式下,1在BITS模式下。分组不适用于 postcript include 样式。

What about this —

$ echo 00: 0123456789abcdef | xxd -r | xxd -g 8 -e | xxd -r | od -tx1
0000000    ef  cd  ab  89  67  45  23  01
0000010

According to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文