【发布时间】:2021-08-01 10:25:04
【问题描述】:
Input Byte array = [b'\x03\x00', b'\x04\x00', b'\x05\x00', b'\x05\x00', b'\x05\x00', b'\x06\x00', b'\x07\x00',
b'\x08\x00', b'\t\x00', b'\n\x00', b'\t\x00', b'\x08\x00', b'\x07\x00', b'\x06\x00']
我需要将上述数组写入二进制文件并验证数据是否正确写入,我正在读回同一个文件。但是在回读时,它给了我一个空列表。有人可以在这里指出错误吗?
#Values array is calculated from the previous steps
intermediate = [int.from_bytes(x, byteorder='big', signed=True) for x in values]
print("Integer in Big Endian {}".format(intermediate))
fileData = [byte.to_bytes(2, byteorder='little', signed=True) for byte in intermediate]
print("Data written to the Binary file{}".format(fileData))
#I am printing the array of bytes that I am writing to the file.
newFile = open('./flash.dat', "wb")
for byte in intermediate:
newFile.write(byte.to_bytes(2, byteorder='little', signed=True))
file = open("./flash.dat", "rb")
number=file.read()
file.close()
print("Data in the binary file {}".format(number))
结果是这样的
Integer in Big Endian [3, 4, 5, 5, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6]
Data written to the Binary file[b'\x03\x00', b'\x04\x00', b'\x05\x00', b'\x05\x00', b'\x05\x00', b'\x06\x00',
b'\x07\x00', b'\x08\x00', b'\t\x00', b'\n\x00', b'\t\x00', b'\x08\x00', b'\x07\x00', b'\x06\x00']
Data in the binary file b''
【问题讨论】:
标签: python python-3.x byte