How to convert ` b’x00x00x00x00x00x00x00x01′ c_ubyte_Array_64 in python. I could find any resources only so any help would really appreciated.
I have to send this message which should be of type c_ubyte_Array_64
JavaScript
x
7
1
import ctypes
2
messages = {
3
"RESET": b'x00x00x00x00x00x00x00x01',
4
}
5
6
ba = bytearray(messages['RESET'])
7
I tried it in the above manner but it gives me this error
JavaScript
1
2
1
TypeError: expected c_ubyte_Array_64 instance, got bytearray
2
Advertisement
Answer
Listing [Python.Docs]: ctypes – A foreign function library for Python.
JavaScript120201>>> import ctypes as ct
2>>>
3>>> messages = {
4"RESET": b"x00x00x00x00x00x00x00x01",
5}
6>>>
7>>> UByteArr64 = ct.c_ubyte * 64 # Create the array type
8>>>
9>>> arr = UByteArr64(*messages["RESET"]) # Create an array instance (by unpacking the byte sequence)
10>>>
11>>> arr
12<__main__.c_ubyte_Array_64 object at 0x0000015C81D74C40>
13>>> arr[0:16]
14[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
15>>>
16>>>
17>>> # Or, in one line:
18>>> (ct.c_ubyte * 64)(*messages["RESET"])
19<__main__.c_ubyte_Array_64 object at 0x0000015C81DBA240>
20