c - Python: How to pack different types of data into a string buffer using struct.pack_into -
i'm trying pack unsigned int data string buffer created using ctypes.create_string_buffer
.
here following code segment, , running example showing error on codepad:
import struct import ctypes import binascii buf = ctypes.create_string_buffer(16) struct.pack_into("=i=i=i", buf, 0, 1, 2, 3) print binascii.hexlify(buf)
this yields following error:
... struct.error: bad char in struct format
the documentation doesn't allude whether can pack data of different types if underlying buffer of specific c type. in case, trying pack unsigned int data string buffer underlying c_char type. know of solution this, or there specific way create buffer can pack type of data?
you're not supposed prefix every output specifier '=' code. once:
struct.pack_into("=iii", buf, 0, 1, 2, 3)
this yields:
01000000020000000300000000000000
Comments
Post a Comment