将二进制数据转换为 C 程序的十六进制字符串
我有一个小的二进制图像,需要用 C 程序表示。表示形式如下:(
static const char[] = {0x1, 0x2, 0x3, 0x4...};
因此,字节将表示为一系列字符)
如何将二进制文件转换为漂亮的 0x..,0x.. 字符串?有一个程序可以做到这一点吗?
I have a small binary image, that needs to be represented in a C program. The representation will be like:
static const char[] = {0x1, 0x2, 0x3, 0x4...};
(so, the bytes will be represented as a series of chars)
How do I convert a binary file into a nice 0x..,0x.. string? Is there a program to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 xxd 实用程序:
将创建
二进制文件。 c
包含以下内容:Use the xxd utility:
Will create
binary.c
with the following content:在这里,更新,有效。通过管道输入文件,它会将其作为十六进制的无符号字符数组吐出到 out.txt。
另一个编辑:我想我不妨把它做得更好。将其打印到 out.txt,这是一个无符号字符数组,格式良好。如果您想向其中添加任何内容,从这里开始应该是微不足道的
Here, updated, works. Pipe in the file, it spits it out as an unsigned char array in hex to out.txt.
Another edit: Figured I might as well make it nice. Prints it out to out.txt, an unsigned char array and nicely formatted. Should be trivial from here if you want to add anything to it
在Python 2.6中
In Python 2.6
如果
pBuff
是您的二进制数据,请尝试计算出缓冲区的长度,例如If
pBuff
is your binary data, try to figure out the length of your buffer, e.g.您可以编写一个程序来非常轻松地完成此操作,但这里有一个 perl 脚本那可以为你做到。
You can write a program to do it pretty easily, but here is a perl script that can do it for you.
你
http://blog.theroyweb.com/embedding -a-binary-file-as-an-array-in-firmware
you
http://blog.theroyweb.com/embedding-a-binary-file-as-an-array-in-firmware
开源应用程序 Hexy 是专门为此设计的。
它在 Windows 和 Linux 上运行。
The open source application Hexy is designed specifically for this.
It runs on Windows and Linux.
效果很好!不过,我做了一个小更改,以允许在命令行上指定输入和输出文件:
Works great! However I made a small change to allow for the input and output files to be specified on the command line:
此 Python 代码/脚本生成二进制 blob 的 C/C++ 字符串。八进制转义符使字符串比十六进制转义符短。
This Python code/script produces a C/C++ string of a binary blob. Octal escapes makes the string shorter than hex escapes.