- python读取二进制是bytes流,通过 “rb” 和 "wb"读取。struct中有pack、unpack等函数可以处理。字节流是多少不一定,看怎么读取。
# 字节翻转
import struct
with open("upload.php", "rb") as f:
fw = open('desktop.zip', 'wb')
while True:
strb = f.read(1)
if not strb:
break
val = struct.unpack("B", strb)[0] # B代表unsigned int(8 bits)
v = ((val & 0xf0) >> 4) + ((val & 0xf) << 4)
s = struct.pack('B', v)
fw.write(s)
fw.close()
- bytes.encode() 转换为str str.decode()转换为bytes