[C++-sig] string conversion / passing binary data

David Abrahams dave at boost-consulting.com
Sun Nov 17 19:23:33 CET 2002


"Achim Domma" <achim at procoders.net> writes:

> Hi,
>
> my ctor for Blob works now (thanks to Dave, Ralf and Mark) and is
> implemented like this:
>
> Blob createBlob(std::string const& s) {
>     // debug output, see below
>     std::cout << "Stringsize:" << s.size() << std::endl;
>     return Blob(s.c_str(),s.size());
> }
>
> void add_class_Blob_to_module() {
>     class_<Blob>("Blob",no_init)
>     ;
>     def("Blob",createBlob);
> }
>
> But now it seems that the conversion to std::string has problems with zeros
> in the python-string. Here is an example python session:
>
>>>> import PythonMagick
>>>> data = open('01.jpg','rb').read()
>>>> b = PythonMagick.Blob(data)
> Stringsize:4
>>>> print len(data)
> 18337
>>>>
>
> As far as I know, std::string is allowed to contain zeros, so is this a bug
> or my fault? Is there a better datatype to pass binary data from python to
> C++?

IIRC this bug has been fixed in the Boost CVS.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list