Converting everything to and from wxString

From WxWiki
Revision as of 19:24, 19 October 2018 by Tierra (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
Jump to navigation Jump to search

This question seems common so I thought I'd write an article. Note that sometimes there may be more than one possible solutions, so don't forget to check the docs.

Literals

A literal is a string written in code with "quotes around it". A literal is not a wxString, and (in wxWidgets 2.8) will not be implicitly converted to one. This means that you can never pass in a raw literal into a wxWidget function or method (unless you don't care about your app not building with Unicode-enabled wxWidgets builds) <source> MessageBox("I'm a mistake!") // WRONG in WxWidgets 2.8 (OK in 3.0) </syntaxhighlight>

Instead, wxWidgets (prior to wxWidgets 3.0) requires you to use one of these macros to turn literals into wxString-compatible characters: <source> _("text that can be translated") wxT("text that won't be translated") _T("same as wxT, but deprecated")

char* c = "sometext"; wxT(c) // WRONG, not a literal </syntaxhighlight>

Rather than being a nuisance, the _(), wxT(), and _T() macros take care of some unicode issues.

char* to wxString

Note that in wxWidgets 3.0, it just works to pass a char array where a wxString parameter is expected, the conversion will be automatic and implicit, using the current locale encoding. So even in wx 3.0, the snippets presented below still make sense when you don't want to be at the mercy of the current locale encoding.

<source> const char* chars = "Hello world";

// if your string is UTF-8 encoded, this is the shortest path : wxString mystring = wxString::FromUTF8(chars);

// You can also convert from many encodings by passing the // appropriate wxConv... parameter to the constructor wxString mystring2(chars, wxConvUTF8); </syntaxhighlight>

wxString to char*

<source> void my_function(const char* foo) { } ... wxString mystring(wxT("HelloWorld")); // you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8) my_function( mystring.mb_str() ); </syntaxhighlight>

mb_str() returns a temporary pointer; if you need the output for more than one function call (as is the case above), you can store the char buffer for a little while :

<source> wxString s( wxT("some string") ); wxCharBuffer buffer=s.ToUTF8(); foo( buffer.data() ); // data() returns const char * bar( buffer.data(), strlen(buffer.data()) ); // in case you need the length of the data </syntaxhighlight>

And if you really need to copy it in to char* (but why would you? ;) :

<source> wxString mystring(wxT("HelloWorld")); char cstring[1024]; // assuming you want UTF-8, change the wxConv* parameter as needed strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023); </syntaxhighlight>

You can also use ToUTF8(), since which encoding you get is clearer than with mb_str()

From const char* to char*: <source> wxString mystring(wxT("HelloWorld")); (const_cast<char*>((const char*)mystring.mb_str())) </syntaxhighlight>

Variadic functions (like printf) won't work with mb_str(), but this will work: <source> wxString mystring(wxT("HelloWorld")); printf("%s",mystring.mb_str().data());

</syntaxhighlight> Alternatively, use the method recommended in Potential Unicode Pitfalls: <source> printf("%s", (const char*)mystring.mb_str()) </syntaxhighlight>

wchar_t* to wxString

<source> const wchar_t* chars = L"Hello world"; wxString mystring(chars); </syntaxhighlight>

wxString to wchar_t*

See the following methods in the docs : <source> wxString::wc_str() wxString::wchar_str() </syntaxhighlight>

wxString to TCHAR

<source> TCHAR tCharString[255]; wxString myString(_T("Hello World")); const wxChar* myStringChars = myString.c_str(); for (int i = 0; i < myString.Len(); i++) {

  tCharString[i] = myStringChars [i];

} tCharString[myString.Len()] = _T('\0'); </syntaxhighlight>

int to wxString

<source> wxString mystring = wxString::Format(wxT("%i"),myint); </syntaxhighlight> or <source> wxString mystring; mystring << myint; </syntaxhighlight>

float to wxString

<source> wxString mystring = wxString::Format(wxT("%f"), myfloat); </syntaxhighlight> or <source> wxString mystring; mystring << myfloat; </syntaxhighlight>

wxString to integer number

<source> wxString number(wxT("145")); long value; if(!number.ToLong(&value)) { /* error! */ } </syntaxhighlight>

or

<source> wxString str = _T("123"); int num;

num = wxAtoi(str); </syntaxhighlight>

wxString to floating-point number

<source> wxString number(wxT("3.14159")); double value; if(!number.ToDouble(&value)){ /* error! */ } </syntaxhighlight>

std::string to wxString

<source> std::string stlstring = "Hello world"; // assuming your string is encoded as UTF-8, change the wxConv* parameter as needed wxString mystring(stlstring.c_str(), wxConvUTF8); </syntaxhighlight>

Starting from wxWidgets 3.0, you may also use the appropriate constructor <source> std::string stlstring = "Hello world"; // assuming your string is encoded as the current locale encoding (wxConvLibc) wxString mystring(stlstring); </syntaxhighlight>

wxString to std::string

wxWidgets 2.8 : <source> wxString mystring(wxT("HelloWorld")); std::string stlstring = std::string(mystring.mb_str()); </syntaxhighlight>

Under wxWidgets 3.0, you may use <source> wxString::ToStdString() </syntaxhighlight>

std::wstring to wxString

Starting from wxWidgets 3.0, you may use the appropriate constructor <source> std::wstring stlstring = L"Hello world"; // assuming your string is encoded as the current locale encoding (wxConvLibc) wxString mystring(stlstring); </syntaxhighlight>

wxString to std::wstring

Under wxWidgets 3.0, you may use <source> wxString::ToStdWstring() </syntaxhighlight>