简体   繁体   English

如何使用linux命令将文本文件转换为二进制文件

[英]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恢复为其二进制表示。

source and source 来源来源

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 : 你可以使用stdinstdout来管道它:

$ 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 ,还应该查看包/命令odhexdump 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.

相关问题 如何将 linux 可执行文件(二进制)转换为 windows exe 文件? - How to convert a linux executable file (binary) to windows exe file? 如何使用linux命令提取与文本文件中特定字段匹配的文本 - how to extract text which matches particular fields in text file using linux commands linux +验证文件是文本还是二进制文件 - linux + verify if file is text or binary 如何在Linux中使用单行Perl命令将数据转换为列并创建CSV文件 - How to convert data into column and create CSV file in Linux using single line Perl commands 如何使用一些 Linux 命令复制一个巨型文件的前几行,并在其末尾添加一行文本? - How to copy the first few lines of a giant file, and add a line of text at the end of it using some Linux commands? 如何在Linux下使用命令行将字符串转换为二进制整数文件 - how to convert string to binary integer file using command line under linux 如何在linux服务器上将二进制文件转换为可读格式 - How to convert binary file into readable format on linux server 如何将二进制文件转换为包含int和chars的文本文件 - How to convert a binary file to a text file, containing ints and chars 将Windows中生成的二进制数据文件转换为Linux - Convert binary data file generated in windows to linux 如何在 Linux/Bash 中提取二进制文件的文本部分? - How can I extract the text portion of a binary file in Linux/Bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM