【发布时间】:2021-09-20 05:11:08
【问题描述】:
我有一个来自随机数生成器的二进制.dat 文件,我需要将其转换为0s 和1s 的字符串。我在 python 中找不到一个优雅的解决方案。
我现在拥有的代码。需要将quantis.dat转换为0s和1s的字符串。
def bytesToBits(bytes):
return bin(int(bytes.hex(), 16))
outfile = open("quantum.txt", "w")
infile = open("quantis.dat", "rb")
chunk = infile.read(1)
while chunk:
decimal = bytesToBits(chunk)
outfile.write(decimal)
chunk = infile.read(1)
outfile.close()
【问题讨论】:
-
您的代码会产生任何错误吗?哪一个?或者生成什么输出?
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: python-3.x file binaryfiles