类型转换
# 按string来显示,byarray代表接收到的数据
readstr = byarray.decode('utf-8')#这样就直接转换成str格式
# 强制转换
readstr = str(byarray)#用这种方式得到的数据会带有b''字符
# 将读取的数据按十六进制字符显示,能让我们直接看到最底层的数据格式
readstr = ' '.join(hex(x) for x in byarray)#这句能把byarray里的数据遍历一遍转换成hex格式,而且用空格相连
# string十六进制转bytearray
bytearray.fromhex(sm[i][:4840])
# str 转 bytes (encode)
str="aabbcc"
bytes=str.encode('utf-8')
# bytes 转 str (decode)
bytes=b"aabbcc"
str=bytes.decode('utf-8')
# bytes 转 bytearray
bytes=b"aabbcc"
byarray=bytearray(bytes)
# str 转 bytearray (encoding)
str="aabbcc"
byarray=bytearray(str)
byarray=bytearray("aabbcc",encoding='utf-8')
str=byarray.decode('utf-8')
bytes=bytes(byarray)
print (byarray)
bytearray(b'aabbcc')
print(str)
aabbcc
print(bytes)
b'aabbcc'
# Python生成十六进制数补零的方法
print('{:02x}'.format(j)) # 输出小写: 0e
print('{:02X}'.format(j)) # 输出大写: 0E
print('0x{:02x}'.format(j)) # 加插0x输出小写: 0x0e
print('0x{:02X}'.format(j)) # 加插0x输出大写: 0x0E
print('0x{:08X}'.format(j)) # 加插0x输出八位: 0x0000000E
进度条
Python实现显示进度条_python 显示进度-优快云博客https://blog.youkuaiyun.com/TaoismHuang/article/details/120747536