Difference between revisions of "Converting everything to and from wxString"

From WxWiki
Jump to navigation Jump to search
m (Text replacement - "<source>" to "<syntaxhighlight lang="cpp">")
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
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.
 
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 ==
 
== 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)
 
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>
+
<syntaxhighlight lang="cpp">
MessageBox("I'm a mistake!")  // WRONG in WxWidgets 2.8 (OK in 2.9)
+
MessageBox("I'm a mistake!")  // WRONG in WxWidgets 2.8 (OK in 3.0)
</source>
+
</syntaxhighlight>
  
Instead, wxWidgets (prior to wxWidgets 2.9) requires you to use one of these macros to turn literals into wxString-compatible characters:
+
Instead, wxWidgets (prior to wxWidgets 3.0) requires you to use one of these macros to turn literals into wxString-compatible characters:
<source>
+
<syntaxhighlight lang="cpp">
 
_("text that can be translated")
 
_("text that can be translated")
 
wxT("text that won't be translated")
 
wxT("text that won't be translated")
_T("same as wxT")
+
_T("same as wxT, but deprecated")
  
 
char* c = "sometext";
 
char* c = "sometext";
 
wxT(c) // WRONG, not a literal
 
wxT(c) // WRONG, not a literal
</source>
+
</syntaxhighlight>
  
Rather than being a nuisance, the [http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#underscore _(), wxT(), and _T() macros] take care of some [http://www.wxwidgets.org/manuals/stable/wx_unicode.html unicode] issues and help with internationalization.
+
Rather than being a nuisance, the [https://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#underscore _(), wxT(), and _T() macros] take care of some [https://www.wxwidgets.org/manuals/stable/wx_unicode.html unicode] issues.
  
 
== char* to wxString ==
 
== char* to wxString ==
<source>
+
 
 +
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.
 +
 
 +
<syntaxhighlight lang="cpp">
 
const char* chars = "Hello world";
 
const char* chars = "Hello world";
// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed
+
 
wxString mystring(chars, wxConvUTF8);
+
// if your string is UTF-8 encoded, this is the shortest path :
</source>
+
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* ==
 
== wxString to char* ==
  
<source>
+
<syntaxhighlight lang="cpp">
 
void my_function(const char* foo)
 
void my_function(const char* foo)
 
{
 
{
Line 38: Line 44:
 
// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)
 
// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)
 
my_function( mystring.mb_str() );
 
my_function( mystring.mb_str() );
</source>
+
</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 :
 
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>
+
<syntaxhighlight lang="cpp">
 
wxString s( wxT("some string") );
 
wxString s( wxT("some string") );
 
wxCharBuffer buffer=s.ToUTF8();
 
wxCharBuffer buffer=s.ToUTF8();
 
foo( buffer.data() );  // data() returns const char *
 
foo( buffer.data() );  // data() returns const char *
bar( buffer.data(), strlen(buffer.data()) );  // in case you need the lenght of the data
+
bar( buffer.data(), strlen(buffer.data()) );  // in case you need the length of the data
</source>
+
</syntaxhighlight>
  
 
And if you really need to copy it in to char* (but why would you? ;) :
 
And if you really need to copy it in to char* (but why would you? ;) :
  
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring(wxT("HelloWorld"));
 
wxString mystring(wxT("HelloWorld"));
 
char cstring[1024];
 
char cstring[1024];
 
// assuming you want UTF-8, change the wxConv* parameter as needed
 
// assuming you want UTF-8, change the wxConv* parameter as needed
 
strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);
 
strncpy(cstring, (const char*)mystring.mb_str(wxConvUTF8), 1023);
</source>
+
</syntaxhighlight>
  
 
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()
  
 
From const char* to char*:
 
From const char* to char*:
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring(wxT("HelloWorld"));
 
wxString mystring(wxT("HelloWorld"));
 
(const_cast<char*>((const char*)mystring.mb_str()))
 
(const_cast<char*>((const char*)mystring.mb_str()))
</source>
+
</syntaxhighlight>
 +
 
 +
Variadic functions (like printf) won't work with mb_str(), but this will work:
 +
<syntaxhighlight lang="cpp">
 +
wxString mystring(wxT("HelloWorld"));
 +
printf("%s",mystring.mb_str().data());
 +
 
 +
</syntaxhighlight>
 +
Alternatively, use the method recommended in [https://docs.wxwidgets.org/trunk/overview_unicode.html#overview_unicode_pitfalls Potential Unicode Pitfalls]:
 +
<syntaxhighlight lang="cpp">
 +
printf("%s", (const char*)mystring.mb_str())
 +
</syntaxhighlight>
  
 
== wchar_t* to wxString ==
 
== wchar_t* to wxString ==
<source>
+
<syntaxhighlight lang="cpp">
 
const wchar_t* chars = L"Hello world";
 
const wchar_t* chars = L"Hello world";
 
wxString mystring(chars);
 
wxString mystring(chars);
</source>
+
</syntaxhighlight>
  
 
== wxString to wchar_t* ==
 
== wxString to wchar_t* ==
 
See the following methods in the docs :
 
See the following methods in the docs :
<source>
+
<syntaxhighlight lang="cpp">
 
wxString::wc_str()  
 
wxString::wc_str()  
 
wxString::wchar_str()
 
wxString::wchar_str()
</source>
+
</syntaxhighlight>
  
 
== wxString to TCHAR ==
 
== wxString to TCHAR ==
<source>
+
<syntaxhighlight lang="cpp">
 
TCHAR tCharString[255];
 
TCHAR tCharString[255];
 
wxString myString(_T("Hello World"));
 
wxString myString(_T("Hello World"));
Line 88: Line 105:
 
}
 
}
 
tCharString[myString.Len()] = _T('\0');
 
tCharString[myString.Len()] = _T('\0');
</source>
+
</syntaxhighlight>
  
 
== int to wxString ==
 
== int to wxString ==
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring = wxString::Format(wxT("%i"),myint);
 
wxString mystring = wxString::Format(wxT("%i"),myint);
</source>
+
</syntaxhighlight>
 
or
 
or
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring;
 
wxString mystring;
 
mystring << myint;
 
mystring << myint;
</source>
+
</syntaxhighlight>
  
 
== float to wxString ==
 
== float to wxString ==
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring = wxString::Format(wxT("%f"), myfloat);
 
wxString mystring = wxString::Format(wxT("%f"), myfloat);
</source>
+
</syntaxhighlight>
 
or
 
or
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring;
 
wxString mystring;
 
mystring << myfloat;
 
mystring << myfloat;
</source>
+
</syntaxhighlight>
  
 
== wxString to integer number ==
 
== wxString to integer number ==
<source>
+
<syntaxhighlight lang="cpp">
 
wxString number(wxT("145"));
 
wxString number(wxT("145"));
 
long value;
 
long value;
 
if(!number.ToLong(&value)) { /* error! */ }
 
if(!number.ToLong(&value)) { /* error! */ }
</source>
+
</syntaxhighlight>
  
 
or
 
or
  
<source>
+
<syntaxhighlight lang="cpp">
 
wxString str = _T("123");
 
wxString str = _T("123");
 
int num;
 
int num;
  
 
num = wxAtoi(str);
 
num = wxAtoi(str);
</source>
+
</syntaxhighlight>
  
 
== wxString to floating-point number ==
 
== wxString to floating-point number ==
  
<source>
+
<syntaxhighlight lang="cpp">
 
wxString number(wxT("3.14159"));
 
wxString number(wxT("3.14159"));
 
double value;
 
double value;
 
if(!number.ToDouble(&value)){ /* error! */ }
 
if(!number.ToDouble(&value)){ /* error! */ }
</source>
+
</syntaxhighlight>
  
 
== std::string to wxString ==
 
== std::string to wxString ==
<source>
+
<syntaxhighlight lang="cpp">
 
std::string stlstring = "Hello world";
 
std::string stlstring = "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(stlstring.c_str(), wxConvUTF8);
 
wxString mystring(stlstring.c_str(), wxConvUTF8);
</source>
+
</syntaxhighlight>
  
Starting from wxWidgets 2.9, you may also use the appropriate constructor
+
Starting from wxWidgets 3.0, you may also use the appropriate constructor
<source>
+
<syntaxhighlight lang="cpp">
 
std::string stlstring = "Hello world";
 
std::string stlstring = "Hello world";
 
// assuming your string is encoded as the current locale encoding (wxConvLibc)
 
// assuming your string is encoded as the current locale encoding (wxConvLibc)
 
wxString mystring(stlstring);
 
wxString mystring(stlstring);
</source>
+
</syntaxhighlight>
  
 
== wxString to std::string ==
 
== wxString to std::string ==
  
 
wxWidgets 2.8 :
 
wxWidgets 2.8 :
<source>
+
<syntaxhighlight lang="cpp">
 
wxString mystring(wxT("HelloWorld"));
 
wxString mystring(wxT("HelloWorld"));
 
std::string stlstring = std::string(mystring.mb_str());
 
std::string stlstring = std::string(mystring.mb_str());
</source>
+
</syntaxhighlight>
  
Under wxWidgets 2.9, you may use
+
Under wxWidgets 3.0, you may use
<source>
+
<syntaxhighlight lang="cpp">
 
wxString::ToStdString()
 
wxString::ToStdString()
</source>
+
</syntaxhighlight>
  
 
== std::wstring to wxString ==
 
== std::wstring to wxString ==
  
Starting from wxWidgets 2.9, you may use the appropriate constructor
+
Starting from wxWidgets 3.0, you may use the appropriate constructor
<source>
+
<syntaxhighlight lang="cpp">
std::sstring stlstring = L"Hello world";
+
std::wstring stlstring = L"Hello world";
 
// assuming your string is encoded as the current locale encoding (wxConvLibc)
 
// assuming your string is encoded as the current locale encoding (wxConvLibc)
 
wxString mystring(stlstring);
 
wxString mystring(stlstring);
</source>
+
</syntaxhighlight>
  
 
== wxString to std::wstring ==
 
== wxString to std::wstring ==
Under wxWidgets 2.9, you may use
+
Under wxWidgets 3.0, you may use
<source>
+
<syntaxhighlight lang="cpp">
 
wxString::ToStdWstring()
 
wxString::ToStdWstring()
</source>
+
</syntaxhighlight>

Latest revision as of 19:36, 19 October 2018

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)

MessageBox("I'm a mistake!")  // WRONG in WxWidgets 2.8 (OK in 3.0)

Instead, wxWidgets (prior to wxWidgets 3.0) 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, but deprecated")

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.

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.

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);

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 the output for more than one function call (as is the case above), you can store the char buffer for a little while :

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

And if you really need to copy it in to 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()

From const char* to char*:

wxString mystring(wxT("HelloWorld"));
(const_cast<char*>((const char*)mystring.mb_str()))

Variadic functions (like printf) won't work with mb_str(), but this will work:

wxString mystring(wxT("HelloWorld"));
printf("%s",mystring.mb_str().data());

Alternatively, use the method recommended in Potential Unicode Pitfalls:

printf("%s", (const char*)mystring.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 3.0, 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 3.0, you may use

wxString::ToStdString()

std::wstring to wxString

Starting from wxWidgets 3.0, you may use the appropriate constructor

std::wstring stlstring = L"Hello world";
// assuming your string is encoded as the current locale encoding (wxConvLibc)
wxString mystring(stlstring);

wxString to std::wstring

Under wxWidgets 3.0, you may use

wxString::ToStdWstring()