转换为二进制
a = struct.pack('cc', b"1", b"2") print(type(a)) print(a) print(type(b"1")) a = struct.pack('bb', 1, 127) # signed char, 范围[-128, 127], 用128会抛异常 print(type(a)) # <class 'bytes'> print(a) # b'\x01\x7f' a = struct.pack('BB', 1, 255) # unsigned char, 范围[0, 255], 用256会抛异常 print(type(a)) # <class 'bytes'> print(a) # b'\x01\xff' a = struct.pack('5?', -12, -1, 0, 1, 255) print(type(a)) # <class 'bytes'> print(a) # b'\x01\x01\x00\x01\x01' a = struct.pack('hh', 1, 255) print(type(a)) # <class 'bytes'> print(a) # b'\x01\x00\xff\x00' a = struct.pack('ii', 1, 255) print(type(a)) # <class 'bytes'> print(a) # b'\x01\x00\x00\x00\xff\x00\x00\x00' a = struct.pack('f', 1.2) print(type(a)) # <class 'bytes'> print(a) # b'\x9a\x99\x99?' a = struct.pack('f', 1) print(type(a)) # <class 'bytes'> print(a) # b'\x00\x00\x80?'
参考
Python教程:如何处理二进制文件 - I'm_江河湖海 - 博客园 (cnblogs.com)
标签:struct,Python,print,工具,x00,type,x01,pack From: https://www.cnblogs.com/sailJs/p/17581798.html