Equivalent of Python `bytes` object


What is the equivalent of a Python bytes object in C++ from the ROOT perspective? So which function should a class exported via a dictionary (linkdef file) define, so that I can pass in a bytes object from python?


What does the byte sequence that you want to pass contain?

For instance, this works (tried in Python2):

>>> import ROOT
>>> ROOT.gInterpreter.Declare('void f(char *s) { cout << s << endl; }')
>>> ROOT.f(bytes("a"))


thanks for the answer! I am using python3 (sorry, should have mentioned that) and your example fails unfortunately:

>>> import ROOT
>>> ROOT.gInterpreter.Declare('void f(char *s) { cout << s << endl; }')
>>> ROOT.f(b"asdf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: void ::f(char* s) =>
    could not convert argument 1

(it also fails when I use std::string). The byte array I have in my real implementation is not ascii-convertable to string…

Indeed I have been trying to find a conversion for bytes in Python3 and so far no luck.

So you can’t just decode your bytes object to a str, pass that string, and do whatever you want to do in C++? What is it that you want to do in C++ with these bytes?

Thanks for your answer!
Unfortunately, it is “real” binary data - so I do not know what to expect. So its not really any textual representation and choosing a proper encoding for this is hard.
Maybe I should think about passing a TBuffer or something…
But thanks anyways!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.