Created
February 2, 2019 21:26
-
-
Save disconnect3d/323009f6cd23b8fd2ec87879615e95ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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