[英]How to convert a text file to binary file using linux commands
I have hex code of a binary in text (string) format. 我有文本(字符串)格式的二进制的十六进制代码。 How do I convert it to a binary file using linux commands like cat and echo ?
如何使用像cat和echo这样的linux命令将其转换为二进制文件?
I know command following command with create a binary test.bin. 我知道命令跟随命令创建二进制test.bin。 But what if this hexcode is in another .txt file ?
但是如果这个十六进制代码在另一个.txt文件中呢? How do I "cat" the content of text file to "echo" and generate a binary file ?
如何“捕捉”文本文件的内容以“回显”并生成二进制文件?
# echo -e "\\x00\\x001" > test.bin
use xxd -r
. 使用
xxd -r
。 it reverts a hexdump to its binary representation. 它将hexdump恢复为其二进制表示。
Edit : The -p
parameter is also very useful. 编辑
-p
参数也非常有用。 It accepts "plain" hexadecimal values, but ignores whitespace and line changes. 它接受“普通”十六进制值,但忽略空格和行更改。
So, if you have a plain text dump like this: 所以,如果您有这样的纯文本转储:
echo "0000 4865 6c6c 6f20 776f 726c 6421 0000" > text_dump
You can convert it to binary with: 您可以将其转换为二进制:
xxd -r -p text_dump > binary_dump
And then get useful output with something like: 然后通过以下方式获得有用的输出:
xxd binary_dump
If you have long text or text in file you can also use the binmake tool that allows you to describe in text format some binary data and generate a binary file (or output to stdout). 如果文件中有长文本或文本,您还可以使用binmake工具,它允许您以文本格式描述一些二进制数据并生成二进制文件(或输出到stdout)。 It allows to change the endianess and number formats and accepts comments.
它允许更改endianess和数字格式并接受评论。
Its default format is hexadecimal but not limited to this. 其默认格式为十六进制,但不限于此。
First get and compile binmake : 首先获取并编译binmake :
$ git clone https://github.com/dadadel/binmake
$ cd binmake
$ make
You can pipe it using stdin
and stdout
: 你可以使用
stdin
和stdout
来管道它:
$ echo '32 decimal 32 61 %x20 %x61' | ./binmake | hexdump -C
00000000 32 20 3d 20 61 |2 = a|
00000005
Or use files. 或者使用文件。 So create your text file
file.txt
: 所以创建你的文本文件
file.txt
:
# an exemple of file description of binary data to generate
# set endianess to big-endian
big-endian
# default number is hexadecimal
00112233
# man can explicit a number type: %b means binary number
%b0100110111100000
# change endianess to little-endian
little-endian
# if no explicit, use default
44556677
# bytes are not concerned by endianess
88 99 aa bb
# change default to decimal
decimal
# following number is now decimal
0123
# strings are delimited by " or '
"this is some raw string"
# explicit hexa number starts with %x
%xff
Generate your binary file file.bin
: 生成二进制文件
file.bin
:
$ ./binmake file.txt file.bin
$ hexdump file.bin -C
00000000 00 11 22 33 4d e0 77 66 55 44 88 99 aa bb 7b 74 |.."3M.wfUD....{t|
00000010 68 69 73 20 69 73 20 73 6f 6d 65 20 72 61 77 20 |his is some raw |
00000020 73 74 72 69 6e 67 ff |string.|
00000027
In addition of xxd
, you should also look at the packages/commands od
and hexdump
. 除了
xxd
,还应该查看包/命令od
和hexdump
。 All are similar, however each provide slightly different options that will allow you to tailor the output to your desired needs. 所有都是相似的,但每个都提供略有不同的选项,使您可以根据您的需要定制输出。 For example
hexdump -C
is the traditional hexdump with associated ASCII translation along side. 例如,
hexdump -C
是传统的hexdump,旁边有相关的ASCII转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.