2

As the title suggests, I'd like to pass a byte array from c++ to python. I understand I would need to convert my byte array into a PyObject somehow but this is where I got stuck. Is there anyone who can help me out with this.

Thanks in advance,

1 Answer 1

3

PyBytes_FromString to the rescue assuming Python3+:

https://docs.python.org/3/c-api/bytes.html

Under Python2.x PyString_FromString:

https://docs.python.org/2/c-api/string.html

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using an opencv matrix to byte conversion like this <br/>std::memcpy(bytes,image.data,size * sizeof(byte)); so this is not a string. How do I use it with PyBytes_FromString?
@las *_FromString functions take const char* as arg. You say that you have a byte array. So I assume that's exactly what it is. It's a bit confusing but strings and byte arrays meant the same thing back in days. If you allow nulls in your sequence of bytes then *_FromStringAndSize might be a better choice.
Anyway I don't know opencv but if you have anything else then first you have to find a way to convert it to char*.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.