Skip to content

Instantly share code, notes, and snippets.

@lmyyao
Last active August 2, 2023 15:03
Show Gist options
  • Save lmyyao/355709b35b717c9e47c6795de7b45ccd to your computer and use it in GitHub Desktop.
Save lmyyao/355709b35b717c9e47c6795de7b45ccd to your computer and use it in GitHub Desktop.
Python ctypes Structure to bytes
from ctypes import *
def convert_bytes_to_structure(st, byte):
# sizoef(st) == sizeof(byte)
memmove(addressof(st), byte, sizeof(st))
def convert_struct_to_bytes(st):
buffer = create_string_buffer(sizeof(st))
memmove(buffer, addressof(st), sizeof(st))
return buffer.raw
def conver_int_to_bytes(number, size):
return (number).to_bytes(size, 'big')
@AbdulTade
Copy link

AbdulTade commented Nov 17, 2021

Thanks needed that. Wanted to transmit a struct over a socket so needed to serialize it to bytes. Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment