Linux 如何在bash中创建仅包含十六进制字符而没有空格的文件的十六进制转储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2614764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a hex dump of file containing only the hex characters without spaces in bash?
提问by David Raswik
How do I create an unmodifiedhex dump of a binary file in Linux using bash? The od
and hexdump
commands both insert spaces in the dump and this is not ideal.
如何使用 bash 在 Linux 中创建二进制文件的未经修改的十六进制转储?在od
与hexdump
命令转储都插入空格,这是不理想的。
Is there a way to simply write a long string with all the hex characters, minus spaces or newlines in the output?
有没有办法在输出中简单地写一个包含所有十六进制字符、减去空格或换行符的长字符串?
回答by Donal Fellows
It seems to depend on the details of the version of od
. On OSX, use this:
这似乎取决于od
. 在 OSX 上,使用这个:
od -t x1 -An file |tr -d '\n '
(That's print as type hex bytes, with no address. And whitespace deleted afterwards, of course.)
(这是打印为十六进制字节类型,没有地址。当然,然后删除空格。)
回答by mark4o
xxd -p file
xxd -p file
Or if you want it all on a single line:
或者,如果您想将所有内容都放在一行中:
xxd -p file | tr -d '\n'
xxd -p file | tr -d '\n'
回答by Alan Haggai Alavi
Perl one-liner:
Perl 单行:
perl -e 'local $/; print unpack "H*", <>' file
回答by Micha? Trybus
Format strings can make hexdump behave exactly as you want it to (no whitespace at all, byte by byte):
格式字符串可以使 hexdump 的行为与您希望的完全一样(根本没有空格,逐字节):
hexdump -ve '1/1 "%.2x"'
1/1
means "each format is applied once and takes one byte", and "%.2x"
is the actual format string, like in printf. In this case: 2-character hexadecimal number, leading zeros if shorter.
1/1
表示“每种格式应用一次并占用一个字节”,并且"%.2x"
是实际的格式字符串,就像在 printf 中一样。在这种情况下:2 个字符的十六进制数,如果较短则前导零。
回答by Paused until further notice.
The other answers are preferable, but for a pure Bash solution, I've modified the script in my answer hereto be able to output a continuous stream of hex characters representing the contents of a file. (Its normal mode is to emulate hexdump -C
.)
其他答案更可取,但对于纯 Bash 解决方案,我在此处的答案中修改了脚本,以便能够输出表示文件内容的连续十六进制字符流。(它的正常模式是模拟hexdump -C
。)