Search code examples
c++wxwidgetsbinary-datawxstring

Convert binary data stored in std::string to wxString


What's the standard way to convert binary data in std::string to a wxString, all the constructors/assignment functions of wxString I've tried so far, even when given a length, are null-terminated which obviously isn't what I want.


Solution

  • Unless I'm missing something, From8BitData should work. For example, assuming str is of type std::string:

    wxString str2 = wxString::From8BitData(str.data(), str.length());
    

    You could then write this to a file like so (assuming outputfile is of type std::ofstream):

    outputfile.write(str2.To8BitData(), str2.length());
    outputfile.close();
    

    I'm not sure what you're gaining by the conversion to wxString. But if you want to do so, that should be how to make the conversion and use the resulting wxString object.