python - How can I convert a BitString to a ctypes Byte Array? -


i've started out bitstring , ctypes, , have part of binary file stored in startdata, bitarray class.

> print(startdata) 0x0000000109f0000000010605ffff 

now, have pass data as-is c function takes unsigned char * argument, i'm first trying this:

buf = (c_ubyte * len(startdata))() 

to this:

buf_ptr = cast(pointer(buf), pointer(c_ubyte)) 

this works, how assign byte data startdata array / buffer created?

this doesn't work:

> buf = (c_ubyte * len(startdata))(*startdata.bytes) typeerror: integer required 

here's possible solution (note i'm using python 3):

import ctypes  def bitarray_to_ctypes_byte_buffer(data):     """convert bitarray instance ctypes array instance"""     ba = bytearray(data.bytes)     ba_len = len(ba)     buffer = (ctypes.c_uint8 * ba_len).from_buffer(ba)     return buffer 

(note: same apply converting bytes instance ctypes byte array, remove .bytes in data.bytes).

you can pass buffer c function using byref:

byref(buffer) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -