gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:02:30 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com