I have some C code passes data to a callback. The callback is defined as:
typedef void (*packetHandlerCb)(uint8_t *data, uint32_t len);
I want to set up a python handler for this callback, so I've done something like:
def handle_packet(packet_data, packet_len):
return bytes(ctypes.cast(packet_data, ctypes.POINTER(ctypes.c_ubyte * packet_len))[0])
PACKET_CB = ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_ubyte), ctypes.c_uint)
cb = PACKET_CB(handle_packet)
I'm curious if there is a more efficient way to convert the C buffer into a python bytes object
It seems like I can simply do bytes(packet_data[:packet_len])
which seems to be about 30% faster on my computer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.