0

I have a c++ application which writes blocks of unsigned char data. So I would be writing unsigned char data[8].

Now, I am using python (read ctypes functionality in python), to read and buffer it in my tool for further processing.

Problem
When I read the data from file and break it down into chunks of 8, all the resultant data is in string format.I have the following structure

class MyData(Union):
     _fields_=[ ("data",8 * c_ubytes), ("overlap", SelfStructure) ]

Now, I am trying to pass the data as follows

  dataObj = MyData(str[0:8])  

It throws an error, expected c_ubyte_Array_8 instance, got str. I think I need to convert string to array of size 8 of c_ubyte. Tried with bytearray but did not succeed. Please let me know how to do.

1 Answer 1

3

Try this:

(ctypes.c_ubyte * 8)(*[ctypes.c_ubyte(ord(c)) for c in str[:8]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.