Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试创建一堆包含相应十六进制值的二进制文件
for i in range(2**8): file = open("test" + str(i) + ".bin", "wb") file.write(hex(i)) file.close()
不幸的是,我的计数器转换为十六进制的文本表示似乎被写入文件而不是实际的十六进制值。有人可以更正此代码吗?我确定问题出在hex(i)
hex(i)
如果您希望以二进制形式写入值,请使用chr()从i创建字符:
for i in range(2**8): with open("test" + str(i) + ".bin", "wb") as f: f.write(chr(i))