读入源文件,将每个字符的十六进制写入新文件,每行仅显示1个字符
实现效果
代码
import os
filepath = r'D:\\360Downloads\\decompression'
with open(filepath + os.path.sep + 'x-ray', 'rb') as f, \
open(filepath + os.path.sep + 'x-ray_8bit.txt', 'w') as outfile:
# f = open(filepath + os.path.sep + 'x-ray', 'rb')
# outfile = open(filepath + os.path.sep + 'x-ray_8bit', 'w')
i = 0
while 1:
c = f.read(1)
i += 1
if not c:
break
# if i % 32 == 0:
# outfile.write('\n')
else:
if ord(c) <= 15:
outfile.write('0' + hex(ord(c))[2:]) #每行只显示*最右边2个符号*
outfile.write('\n')
else:
outfile.write(hex(ord(c))[2:])
outfile.write('\n')
# f.close()
# outfile.close()