Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Created February 2, 2019 21:26
Show Gist options
  • Save disconnect3d/323009f6cd23b8fd2ec87879615e95ff to your computer and use it in GitHub Desktop.
Save disconnect3d/323009f6cd23b8fd2ec87879615e95ff to your computer and use it in GitHub Desktop.
import ctypes
class MyStruct(ctypes.LittleEndianStructure):
_fields_ = (
('x', ctypes.c_float),
('y', ctypes.c_float),
('velocity', ctypes.c_int32),
('weight', ctypes.c_uint32)
)
def __str__(self):
return f'MyStruct(x={self.x}, y={self.y}, v={self.velocity}, w={self.weight})'
data = MyStruct(x=1.2345, y=5e1337, velocity=-30, weight=70)
serialized = bytes(data)
print(serialized)
deserialized = MyStruct.from_buffer_copy(serialized)
print(data, deserialized)
print(data == deserialized)
print(data is deserialized)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment