Last active
August 2, 2023 15:03
-
-
Save mydreambei-ai/355709b35b717c9e47c6795de7b45ccd to your computer and use it in GitHub Desktop.
Python ctypes Structure to bytes
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
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks needed that. Wanted to transmit a struct over a socket so needed to serialize it to bytes. Thanks again