当我将txt文件转换为字节操作时,我遇到了一些问题。
little
是`test.txt中唯一的单词。 ascii
代码编码的结果为6c6974746c65
。 我想像data[0x2]|data[0x3]<<8 means 0x74 | (0x74)<<8 =0x7474
一样执行字节操作。
f = open('test.txt','rb')
output = f.read()
f.close()
file_size = len(output)
print output,type(output)
output_ascii = output.encode("hex")
print output_ascii , type(output_ascii) , len(output_ascii)
print output_ascii[0],output_ascii[1]`
结果
little ('type str')
6c6974746c65 (type 'str') 12
6 c
我想得到output_ascii [0] = 0x6c , output_ascii[1] = 0x69 ...etc
我该如何修改?
答案 0 :(得分:0)
byte = ''
total_byte = []
with open('test.txt','rb') as f:
output = f.read()
print output, type(output)
for ele in output:
byte = ele.encode("hex")
total_byte.append(byte)
print byte ,type(byte)
print total_byte[0], total_byte[1]