How to pass on arrays of raw bytes? #6061
Replies: 1 comment
-
|
The root bug is this loop: for(uint i; i < m.getLength(); ++i)
Adding Fix: for (uint i = 0; i < m.getLength(); ++i)That said, if the goal is to return raw bytes to Python, .def("getValue", [](const rav::Msg &m) {
auto data = m.getValue();
return py::bytes(reinterpret_cast<const char *>(data.get()),
static_cast<pybind11::ssize_t>(m.getLength()));
})Python will receive a If you need signed |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm implementing a python module that allows to create a Msg data type. It takes
integers and an array of bytes. When I call the toString method of my type, it
prints all the values:
T: 17, L: 13, V: [2,3,5,7,11,13,17,19,23,127,-128,-127,0], S: [17,0,0,0,13,2,3,5,7,11,13,17,19,23,127,-128,-127,0]
V is the actual value and I've got a method that returns that value. However it returns
a shared_prt<int8_t[]), so I created a lambda that shall return a vector holding those
byte values:
Calling it prints an empty vector:
print(f"value: {msg.getValue()}")
value: []
OK, maybe I did make some mistakes initialising it. So I added a few cout's:
The method execution prints:
, 1, 1, 1, 1, 11, 13, 17, 19, 23, 127, -128, -127, 1
vector size: 13
, 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 127 , -128 , -127 , 0
And the script returns:
print(f"value: {msg.getValue()}")
value: [2, 3, 5, 7, 11, 13, 17, 19, 23, 127, -128, -127, 0]
I thought, maybe the code without using the variable gets optimised away (using -O2 as compiler flag).
However changing it to -O0 did not make any difference.
I'm new to C++ and Python, so I'm clearly doing it wrong. What is the correct way
of passing an array of bytes (that may contain 0 values) to a C++ constructor
or method or returning the such a byte array?
Beta Was this translation helpful? Give feedback.
All reactions