用于反转字节顺序/更改字节顺序的命令行

发布于 2024-11-16 09:32:30 字数 443 浏览 3 评论 0原文

我在一些脚本中尝试解析由 Java DataOutputStream#writeLong(...) 编写的一些数据。由于java似乎总是写大端,所以我在向od提供字节时遇到问题。这是因为 od 始终假设字节序与您当前所在的架构的字节序相匹配,而我使用的是小型字节序机器。

我正在寻找一种简单的单行来反转字节顺序。假设您知道文件的最后 8 个字节是由上述 writeLong(...) 方法写入的 long。我目前打印这么长的最佳尝试是

tail -c 8 file | tac | od -t d8

,但 tac 似乎只适用于文本(很公平)。我发现了一些对 dd conv=swab 的引用,但这只能成对交换字节,并且不能反转这八个字节。

有谁知道这方面的好单行本吗?

I'm hacking around in some scripts trying to parse some data written by Javas DataOutputStream#writeLong(...). Since java always seems to write big endian, I have a problem feeding the bytes to od. This is due to the fact that od always assumes that the endianess matches the endianess of the arch that you are currently on, and I'm on a little endian machine.

I'm looking for an easy one-liner to reverse the byte order. Let's say that you know that the last 8 bytes of a file is a long written by the aforementioned writeLong(...) method. My current best attempt to print this long is

tail -c 8 file | tac | od -t d8

, but tac only seems to work on text (fair enough). I've found some references to dd conv=swab, but this only swaps bytes in pairs, and cannot reverse these eight bytes.

Does anyone know a good one-liner for this?

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

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

发布评论

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

评论(9

删除→记忆 2024-11-23 09:32:30

您可以使用 objcopy:

$ objcopy -I binary -O binary --reverse-bytes=num inputfile.bin outputfile.bin

其中 num 是 2 或 4。

You could use objcopy:

$ objcopy -I binary -O binary --reverse-bytes=num inputfile.bin outputfile.bin

where num is either 2 or 4.

陌伤浅笑 2024-11-23 09:32:30

用过dd,卢克!

dd if=sourcefile of=resultfile conv=swab

Used dd, Luke!

dd if=sourcefile of=resultfile conv=swab
月牙弯弯 2024-11-23 09:32:30

最后求助于Perl。使用了我在 PERL One Liners 中找到的单行代码:

tail -c 8 file | perl -0777e 'print scalar reverse <>' | od -t d8

0777 分隔符让我有点困惑,但是 debian admin 的 this 页面似乎表明它是“no”的占位符记录分隔符,触发每个字节的完全反向字节。

欢迎其他建议。

编辑:在 tac.c 的评论中找到另一个命令,我从 GNU coreutils 下载了该命令:

复制每个文件,或者标准输入(如果没有给出)或者当
遇到FILE名“-”,到标准输出用
记录顺序颠倒。记录由以下分隔符分隔
字符串的实例,如果没有给出则为换行符。默认情况下,
分隔符字符串附加到记录的末尾
文件中如下。

选项:
-b, --before 分隔符附加到开头
文件中它前面的记录的名称。
-r, --regex 分隔符是正则表达式。
-s, --separator=separator 使用 SEPARATOR 作为记录分隔符。

要逐字节反转文件,请使用(在 bash、ksh 或 sh 中):
tac -r -s '.\|
' 文件

Resorted to Perl in the end. Used a one-liner which I found at PERL One Liners:

tail -c 8 file | perl -0777e 'print scalar reverse <>' | od -t d8

The 0777 separator char was a bit puzzling to me, but this page at debian admin seems to suggest that it is a placeholder for 'no record separator', triggering a complete reverse byte-per byte.

Other suggestions are welcome.

EDIT: Found another command in a comment to tac.c, which I downloaded from GNU coreutils:

Copy each FILE, or the standard input if none are given or when a
FILE name of "-" is encountered, to the standard output with the
order of the records reversed. The records are separated by
instances of a string, or a newline if none is given. By default, the
separator string is attached to the end of the record that it
follows in the file.

Options:
-b, --before The separator is attached to the beginning
of the record that it precedes in the file.
-r, --regex The separator is a regular expression.
-s, --separator=separator Use SEPARATOR as the record separator.

To reverse a file byte by byte, use (in bash, ksh, or sh):
tac -r -s '.\|
' file

故事与诗 2024-11-23 09:32:30

请注意,下一版本的 GNU coreutils (>= 8.23) 将在 od 命令中添加 --endian={little,big} 选项

Note the next version of GNU coreutils (>= 8.23) will add the --endian={little,big} option to the od command

难以启齿的温柔 2024-11-23 09:32:30

我想出了这个 Perl 单行代码将 4 字节整数从一种字节序转换为另一种字节序:

$ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin > littlend.bin

这可能在真正的 Linux 机器上工作得很好,但 Cygwin 最后咬了我,将二进制文件视为文本并插入 0x0D(又名CR)在每个 0x0A 字节(又名换行符)之前。但如果你通过管道传输到cat -,似乎就不管它了。这对我有用:

$ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin | cat - > littlend.bin

I came up with this Perl one-liner to convert 4-byte integers from one endianness to another:

$ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin > littlend.bin

That probably works fine on real Linux machines, but Cygwin bit me in the end, treating the binary file as text and inserting a 0x0D (aka CR) before each 0x0A byte (aka newline). But if you pipe to cat -, it seems to leave it alone. This works for me:

$ perl -e 'open F,shift; do { read(F,$a,4); print scalar reverse($a);} while(!eof(F));' bigend.bin | cat - > littlend.bin
羁〃客ぐ 2024-11-23 09:32:30

BASH:

od -b -v -w8 | while read pfx b8 ; do [ "$b8" ] && echo -n 12345678 | tr 87654321 \\${b8// /\\} ; done

为了根据 od 的输出样式变得更加健壮,它可能需要压缩空格(插入 "| sed 's/ */ /g'"w8 之后)。

BASH:

od -b -v -w8 | while read pfx b8 ; do [ "$b8" ] && echo -n 12345678 | tr 87654321 \\${b8// /\\} ; done

To be a bit more robust depending on the output style of od, it may need to compress spaces ( insert "| sed 's/ */ /g'" after the w8).

迷乱花海 2024-11-23 09:32:30

xxd 有两个标志 -e-g 以满足您的目的。

    -e          little-endian dump (incompatible with -ps,-i,-r).
    -g          number of octets per group in normal output. Default 2 (-e: 4).

这样,您可以执行以下操作:

tail -c 8 file | xxd -e -g8

xxd has two flags -e and -g for your purpose.

    -e          little-endian dump (incompatible with -ps,-i,-r).
    -g          number of octets per group in normal output. Default 2 (-e: 4).

This way, you can do:

tail -c 8 file | xxd -e -g8
子栖 2024-11-23 09:32:30

一种简单的 Python 方法,每 4 个字节反转一次。使用较新的 Python 3.8+ walrus 运算符:

import sys
while word := sys.stdin.buffer.read(4):
    sys.stdout.buffer.write(bytes(reversed(word)))

上面的内容很容易理解,但如果您想要更紧凑的 oneliner,您可以将上面的脚本修改为:

python3 -c 

注意 $'' 表示法假设您正在使用重击。它允许您在 Python 命令中使用换行符。

例如,在标准输入上交换一些字符:

# echo ABCDEFGH | python3 -c 
import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'

注意 $'' 表示法假设您正在使用重击。它允许您在 Python 命令中使用换行符。

例如,在标准输入上交换一些字符:


import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'
DCBAHGFE
import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'

注意 $'' 表示法假设您正在使用重击。它允许您在 Python 命令中使用换行符。

例如,在标准输入上交换一些字符:

A simple Python approach that reverses every 4 bytes. Uses newer Python 3.8+ walrus operator:

import sys
while word := sys.stdin.buffer.read(4):
    sys.stdout.buffer.write(bytes(reversed(word)))

The above is simple to understand, but if you want a more compact oneliner, you can modify the above script into:

python3 -c 

Note the $'' notation assumes you're using bash. It allows you to use newlines in the Python command.

As an example, swapping some chars on stdin:

# echo ABCDEFGH | python3 -c 
import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'

Note the $'' notation assumes you're using bash. It allows you to use newlines in the Python command.

As an example, swapping some chars on stdin:


import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'
DCBAHGFE
import sys\nwhile word := sys.stdin.buffer.read(4):\n sys.stdout.buffer.write(bytes(reversed(word)))'

Note the $'' notation assumes you're using bash. It allows you to use newlines in the Python command.

As an example, swapping some chars on stdin:

无戏配角 2024-11-23 09:32:30

我发现这个命令对于交换 4 字节字节顺序效果很好

xxd -e input.bin | xxd -r > output_bs.bin

I found that this command worked well for swapping 4 bytes for endianness

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