1 import struct 2 from ctypes import * 3 4 5 class MyStruct(Structure): 6 _fields_ = [ 7 ("v1", c_char), # c_byte 8 ("v2", c_char), # c_byte 9 ("v1_dire", c_char), # c_byte 10 ("v2_dire", c_char) # c_byte 11 ] 12 13 14 mys = MyStruct() 15 mys.v1 = 20 16 print(type(mys.v1)) 17 mys.v2 = 30 18 mys.v1_dire = 0 19 mys.v2_dire = 0 20 msg = struct.pack('cccc', mys.v1, mys.v2, mys.v1_dire, mys.v2_dire) # BBBB 21 print(msg) 22 ss = struct.unpack('cccc', msg) # BBBB