What is the correct way of converting a wxString? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

What is the correct way of converting a wxString?

Post by tomay3000 »

Hello,
I have written a simple Serial Port wrapper that takes many read function overloads, one of these overloads takes a wxString as parameter.
I have learned that I should pass the writable buffer of the wxString like this:

Code: Select all

wxString strBuf('\0', 1024);
wxStringBuffer(strBuf, strBuf.size());
This worked without problems, but the read result was incorrect (that is encoded), so I have debugged it using many wxString conversion functions and/or castings:

Code: Select all

wxString str1(strBuf.data());
wxString str2((const char *)strBuf.data());
wxString str3((const unsigned char *)strBuf.data());

wxString str4(strBuf.wc_str());
wxString str5((const char *)strBuf.wc_str());
wxString str6((const unsigned char *)strBuf.wc_str());

wxString str7(strBuf.mb_str());
wxString str8((const char *)strBuf.mb_str());
//wxString str9((const unsigned char *)strBuf.mb_str());

wxString str10(strBuf.wx_str());
wxString str11((const char *)strBuf.wx_str());
wxString str12((const unsigned char *)strBuf.wx_str());

wxString str13(strBuf.utf8_str());
wxString str14((const char *)strBuf.utf8_str());
//wxString str15((const unsigned char *)strBuf.utf8_str());

wxString str16 = wxString::FromUTF8(strBuf.data());
wxString str17 = wxString::FromUTF8((const char *)strBuf.data());
//wxString str18 = wxString::FromUTF8((const unsigned char *)strBuf.data());

//wxString str19 = wxString::FromAscii(strBuf.wc_str());
wxString str20 = wxString::FromAscii((const char *)strBuf.wc_str());wxString strBuf(strBuf_Tmp);
wxString str21 = wxString::FromAscii((const unsigned char *)strBuf.wc_str());

wxString str22 = wxString::From8BitData(strBuf.mb_str());
wxString str23 = wxString::From8BitData((const char *)strBuf.mb_str());
//wxString str24 = wxString::From8BitData((const unsigned char *)strBuf.mb_str());
The commented lines were due to compiler errors.

and here is the Watches screenshot:
Untitled.png
Untitled.png (33.86 KiB) Viewed 1075 times
As you can see all the blue framed wxStrings are correct.

What wxString conversion should I use in this case?
Can I use an in-place conversion (that is without creating a new wxString) ?

TIA
Last edited by tomay3000 on Fri Sep 13, 2019 10:25 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the correct way of converting a wxString?

Post by doublemax »

What type is strBuf? It looks like it's a wxString which would be wrong. wxString is unsuitable as a generic byte container.

If you're receiving data from an external source, you should have a (unsigned) char buffer and and number of valid bytes in it. (or a wxMemoryBuffer if you want to use a wx class for it). Then, assuming the data is UTF8 encoded use FromUTF8() to convert the data into a wxString.
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: What is the correct way of converting a wxString?

Post by tomay3000 »

strBuf is indeed a wxString used as container, I have updated my question.
And yes I am receiving data from an external device.

And only these conversions worked for me (notice the cast), any other conversion without any cast didn't work

Code: Select all

wxString str5((const char *)strBuf.wc_str());
wxString str6((const unsigned char *)strBuf.wc_str());

wxString str11((const char *)strBuf.wx_str());
wxString str12((const unsigned char *)strBuf.wx_str());

wxString str20 = wxString::FromAscii((const char *)strBuf.wc_str());wxString strBuf(strBuf_Tmp);
wxString str21 = wxString::FromAscii((const unsigned char *)strBuf.wc_str());
If I am willing to use wxString later which one from the six that worked should I use?

I will try using wxMemoryBuffer BTW.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: What is the correct way of converting a wxString?

Post by doublemax »

Converting a wxString into another wxString using any of these methods is just bad. Don't do it.

Append all incoming data into a wxMemoryBuffer and then convert the data to a wxString.
Use the source, Luke!
tomay3000
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Mon Apr 24, 2017 4:23 am

Re: What is the correct way of converting a wxString?

Post by tomay3000 »

Fair enough.
Thank you.
Post Reply