Difference between revisions of "Converting everything to and from wxString"
Line 23: | Line 23: | ||
== char* to wxString == | == char* to wxString == | ||
<source> | <source> | ||
− | char* chars = "Hello world"; | + | const char* chars = "Hello world"; |
// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed | // assuming your string is encoded as UTF-8, change the wxConv* parameter as needed | ||
wxString mystring(chars, wxConvUTF8); | wxString mystring(chars, wxConvUTF8); | ||
Line 50: | Line 50: | ||
You can also use '''ToUTF8()''', since which encoding you get is clearer than with mb_str() | You can also use '''ToUTF8()''', since which encoding you get is clearer than with mb_str() | ||
+ | |||
+ | == wchar_t* to wxString == | ||
+ | <source> | ||
+ | const wchar_t* chars = L"Hello world"; | ||
+ | wxString mystring(chars); | ||
+ | </source> | ||
+ | |||
+ | == wxString to wchar_t* == | ||
+ | See the following methods in the docs : | ||
+ | <source> | ||
+ | wxString::wc_str() | ||
+ | wxString::wchar_str() | ||
+ | </source> | ||
== wxString to TCHAR == | == wxString to TCHAR == | ||
Line 131: | Line 144: | ||
<source> | <source> | ||
wxString::ToStdString() | wxString::ToStdString() | ||
+ | </source> | ||
+ | |||
+ | == std::wstring to wxString == | ||
+ | |||
+ | Starting from wxWidgets 2.9, you may use the appropriate constructor | ||
+ | <source> | ||
+ | std::sstring stlstring = L"Hello world"; | ||
+ | // assuming your string is encoded as the current locale encoding (wxConvLibc) | ||
+ | wxString mystring(stlstring); | ||
+ | </source> | ||
+ | |||
+ | == wxString to std::wstring == | ||
+ | Under wxWidgets 2.9, you may use | ||
+ | <source> | ||
+ | wxString::ToStdWstring() | ||
</source> | </source> |
Revision as of 19:41, 17 November 2010
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.
Note that it is recommended to use wxString as much as possible. Do no use char* or std::string unless you use a third-party library that requires you to do so.
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)
MessageBox("I'm a mistake!") // WRONG in WxWidgets 2.8 (OK in 2.9)
Instead, wxWidgets (prior to wxWidgets 2.9) requires you to use one of these macros to turn literals into wxString-compatible characters:
_("text that can be translated")
wxT("text that won't be translated")
_T("same as wxT")
char* c = "sometext";
wxT(c) // WRONG, not a literal
Rather than being a nuisance, the _(), wxT(), and _T() macros take care of some unicode issues and help with internationalization.
char* to wxString
const char* chars = "Hello world";
// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed
wxString mystring(chars, wxConvUTF8);
wxString to char*
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() );
mb_str() returns a temporary pointer. If you need to store it in a char* (but why would you? ;) :
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);
You can also use ToUTF8(), since which encoding you get is clearer than with mb_str()
wchar_t* to wxString
const wchar_t* chars = L"Hello world";
wxString mystring(chars);
wxString to wchar_t*
See the following methods in the docs :
wxString::wc_str()
wxString::wchar_str()
wxString to TCHAR
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');
int to wxString
wxString mystring = wxString::Format(wxT("%i"),myint);
or
wxString mystring;
mystring << myint;
float to wxString
wxString mystring = wxString::Format(wxT("%f"), myfloat);
or
wxString mystring;
mystring << myfloat;
wxString to integer number
wxString number(wxT("145"));
long value;
if(!number.ToLong(&value)) { /* error! */ }
or
wxString str = _T("123");
int num;
num = wxAtoi(str);
wxString to floating-point number
wxString number(wxT("3.14159"));
double value;
if(!number.ToDouble(&value)){ /* error! */ }
std::string to wxString
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);
Starting from wxWidgets 2.9, you may also use the appropriate constructor
std::string stlstring = "Hello world";
// assuming your string is encoded as the current locale encoding (wxConvLibc)
wxString mystring(stlstring);
wxString to std::string
wxWidgets 2.8 :
wxString mystring(wxT("HelloWorld"));
std::string stlstring = std::string(mystring.mb_str());
Under wxWidgets 2.9, you may use
wxString::ToStdString()
std::wstring to wxString
Starting from wxWidgets 2.9, you may use the appropriate constructor
std::sstring stlstring = L"Hello world";
// assuming your string is encoded as the current locale encoding (wxConvLibc)
wxString mystring(stlstring);
wxString to std::wstring
Under wxWidgets 2.9, you may use
wxString::ToStdWstring()