python 字节到 C 数组(如 xxd 程序)

标签 python arrays python-3.x byte

在 python3 中我有一些字节。我想将它们导出为 C 源代码。 在 python 之外,我使用“xxd -i binary_file”命令。

示例:

x = b'abc123'
print(bytes_to_c_arr(x))
# should output:
unsigned char x[] = { 0x61, 0x62, 0x63, 0x31, 0x32, 0x33 };

有现成的方法或方便的单行文字吗?我可以不需要类型声明,只需要内容的字节就足够了。

最佳答案

1. print([hex(i) for in x]) 

2. print(a.hex())

结果将是:

1. ['0x61', '0x62', '0x63', '0x31', '0x32', '0x33']
2. '616263313233'

关于python 字节到 C 数组(如 xxd 程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449430/

相关文章:

python - 打开串行端口或使用 pyserial 从串行端口读取时遇到问题

将二维数组的行复制到一维数组

python - 如何将 flask-sqlalchemy 与 Google Cloud Functions 一起使用?

Python numpy MemoryError - 将多个 CSV 文件加载到 HDF5 存储中并读入 DataFrame

python线程事件: why do we need clear()

python - 有效地将每个图像像素除了最大的 n 个元素之外的所有元素清零

python - Pandas :在最大距离内寻找点

python - 正则表达式查找特定文件路径

Javascript - 用于展平数组的递归/for 循环

arrays - 如何在 Rust 1.0 中的堆上分配数组?