在shell脚本中将二进制数据转换为十六进制
我想将二进制数据转换为十六进制,仅此而已,没有花哨的格式等等。 hexdump
似乎太聪明了,它对我来说“过度格式化”。我想从 /dev/random 中获取 x 个字节并将它们作为十六进制传递。
最好我只想使用标准的 Linux 工具,这样我就不需要在每台机器上安装它(有很多)。
I want to convert binary data to hexadecimal, just that, no fancy formatting and all. hexdump
seems too clever, and it "overformats" for me. I want to take x bytes from the /dev/random and pass them on as hexadecimal.
Preferably I'd like to use only standard Linux tools, so that I don't need to install it on every machine (there are many).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
也许使用 xxd:
Perhaps use
xxd
:当心!
hexdump
和xxd
给出了不同字节顺序的结果!简单解释一下。大尾数与小尾数 :D
Watch out!
hexdump
andxxd
give the results in a different endianness!Simply explained. Big-endian vs. little-endian :D
使用 od (GNU 系统):
使用 hexdump (BSD 系统):
来自 十六进制转储、od 和 hexdump:
With od (GNU systems):
With hexdump (BSD systems):
From Hex dump, od and hexdump:
也许您可以用 C 编写自己的小工具,并即时编译它:
然后从标准输入提供它:
您甚至可以使用 heredoc 语法将这个小 C 程序嵌入到 shell 脚本中。
Perhaps you could write your own small tool in C, and compile it on-the-fly:
And then feed it from the standard input:
You can even embed this small C program in a shell script using the heredoc syntax.
所有的解决方案似乎都很难记住或太复杂。我发现使用 printf 是最短的:
但正如评论中所述,这不是作者想要的,所以公平地说,下面是完整的答案。
...使用上面的方法输出实际的二进制数据流:
它的作用:
printf '%x,' 1 2 3, 将打印
1,2,3,
cat /dev/urandom
- 它输出随机二进制数据head -c 5
- 将二进制数据限制为 5 个字节od -An -vtu1
- 八进制转储命令,将二进制转换为十进制作为测试用例('a' 是 61 十六进制,'p' 是 70 十六进制, ...):
或者要测试单个二进制字节,在输入时让我们给出 61 个十进制('=' 字符)来生成二进制数据(
'\\x%x'
格式即可实现)。上面的命令将正确输出3d(十进制61):All the solutions seem to be hard to remember or too complex. I find using
printf
the shortest one:But as noted in comments, this is not what author wants, so to be fair, below is the full answer.
... to use above to output actual binary data stream:
What it does:
printf '%x,' 1 2 3
, will print1,2,3,
cat /dev/urandom
- it outputs random binary datahead -c 5
- limits binary data to 5 bytesod -An -vtu1
- octal dump command, converts binary to decimalAs a testcase ('a' is 61 hex, 'p' is 70 hex, ...):
Or to test individual binary bytes, on input let’s give 61 decimal ('=' char) to produce binary data (
'\\x%x'
format does it). The above command will correctly output 3d (decimal 61):如果您需要大流(没有换行符),您可以使用 tr 和 xxd (Vim 的一部分)进行逐字节转换。
或者您可以使用
hexdump
(POSIX) 逐字查看单词转换。请注意,区别在于字节顺序。
If you need a large stream (no newlines) you can use
tr
andxxd
(part of Vim) for byte-by-byte conversion.Or you can use
hexdump
(POSIX) for word-by-word conversion.Note that the difference is endianness.
dd + hexdump 也可以工作:
dd + hexdump will also work:
有时,如果您针对多个平台,perl5 的可移植性会更好。每个 Linux 发行版和 Unix 操作系统都附带它。您通常可以在容器映像中找到它,而 xxd 或 hexdump 等其他工具不可用。下面是如何在 Perl 中执行相同的操作:
请注意,这会更多地使用 slurp,这会导致 Perl 将整个输入读取到内存中,当输入很大时,这可能不是最佳的。
Sometimes perl5 works better for portability if you target more than one platform. It comes with every Linux distribution and Unix OS. You can often find it in container images where other tools like xxd or hexdump are not available. Here's how to do the same thing in Perl:
Note that this uses slurp more, which causes Perl to read the entire input into memory, which may be suboptimal when the input is large.
这三个命令将打印相同的内容 (0102030405060708090a0b0c):
鉴于
n=12
和$a
是从 1 到 26 的字节值:这可用于获取
$n 随机字节值:
These three commands will print the same (0102030405060708090a0b0c):
Given that
n=12
and$a
is the byte values from 1 to 26:That could be used to get
$n
random byte values in each program: