在 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/