Skip to content

Instantly share code, notes, and snippets.

@iamadou
Forked from mydreambei-ai/convert_struct_to_bytes.py
Created September 20, 2019 07:43
Show Gist options
  • Select an option

  • Save iamadou/8a894c7c20c98b08c8c62e9583bb5d5b to your computer and use it in GitHub Desktop.

Select an option

Save iamadou/8a894c7c20c98b08c8c62e9583bb5d5b 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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment