How do I build a python string from a raw (binary) ctype buffer? -
i'm playing python , ctypes , can't figure out how resolve problem. call c function fills raw binary data. code looks this:
class client(): def __init__(self): self.__bufsize = 1024*1024 self.__buf = ctypes.create_string_buffer(self.__bufsize) self.client = ctypes.cdll.loadlibrary(r"i:\bin\client.dll") def do_something(self): len_written = self.client.fill_raw_buffer(self.__buf, self.__bufsize) my_string = repr(self.__buf.value) print my_string
the problem i'm receiving binary data (with 0x00) , it's truncated when tried build my_string. how can build my_string if self._buf contains null bytes 0x00?
any idea welcome. thanks
you can access buffer returned create_string_buffer()
python string using raw
attribute:
a = ctypes.create_string_buffer(10) a.raw # '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
to access first n
bytes, use
a.raw[:n]
Comments
Post a Comment