【发布时间】:2015-01-03 23:32:15
【问题描述】:
我正在尝试将取自 ascii 文件的十六进制数据写入新创建的二进制文件
ascii 文件示例:
98 af b7 93 bb 03 bf 8e ae 16 bf 2e 52 43 8b df
4f 4e 5a e4 26 3f ca f7 b1 ab 93 4f 20 bf 0a bf
82 2c dd c5 38 70 17 a0 00 fd 3b fe 3d 53 fc 3b
28 c1 ff 9e a9 28 29 c1 94 d4 54 d4 d4 ff 7b 40
我的代码
hexList = []
with open('hexFile.txt', 'r') as hexData:
line=hexData.readline()
while line != '':
line = line.rstrip()
lineHex = line.split(' ')
for i in lineHex:
hexList.append(int(i, 16))
line = hexData.readline()
with open('test', 'wb') as f:
for i in hexList:
f.write(hex(i))
认为hexList 已经保存了十六进制转换的数据,f.write(hex(i)) 应该将这些十六进制数据写入文件,但 python 以 ascii 模式写入
最终输出:0x9f0x2c0x380x590xcd0x110x7c0x590xc90x30xea0x37 这是错误的!
问题出在哪里?
【问题讨论】:
标签: python string file binary hex