c++ – How to work with wxString, numbers and other string types in wxWidgets

c++ – How to work with wxString, numbers and other string types in wxWidgets

First of all, there is this helpfull site in the wxWidgets wiki on how to deal with wxStrings. I will partly quote it, though in my opinion it is not as detailed as I would have needed it, this is why I created this Q&A.

How to convert wxString to another string type?

For C style strings, I use this method:

wxString fileName = myFile;
const char* fileNameChar = fileName.mb_str();

To convert wxString to std::string use (as the website says):

wxWidgets 2.8 :

wxString mystring(wxT(HelloWorld));
std::string stlstring = std::string(mystring.mb_str());

Under wxWidgets 3.0, you may use


And for std::wstring under wxWidgets 3.0, you may use

wxString myString = abc;
std::wstring myWString = wxString::ToStdWstring(myString);

(not tested, documented here).

How to convert from another string type?

To convert from C style strings use:

const char* fileNameChar = myFile;
wxString fileName(fileNameChar);

For std::strings either

std::string fileNameStd = myFile;
wxString fileName(fileNameStd.c_str());

or (from wxWidgets 3.0)

wxString fileName(fileNameStd);

And for std::wstring:

Starting from wxWidgets 3.0, you may use the appropriate constructor

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

How to convert an integer number to wxString?

You can either use

int number = 3;
wxString myString = wxString::Format(wxT(%i), number);


#include <wx/numformatter.h>

int number = 3;
wxString myString = wxNumberFormatter::ToString(number);

The second method is documented here. You dont have to use the flag and you can use not only long as it is in the documentation, but other integer types as well (as I did here with int).

How to convert wxString to an integer number?

I always use this method:

wxString numberString = 12345;
int number = wxAtoi(numberString);

How to convert a floating point number to wxString?

You can use the first method if you dont need to set the accurracy of your floating point values (normal accurracy is 6 numbers after the comma)

double doubleNumber = 12.3455;
wxString numberString = wxString::Format(wxT(%f), doubleNumber);

Be carefull, as it is %f no matter if you want to convert a double or a float number. If you try to use %d instead your program will crash. If you use this method to convert anything that has more than 6 digits after the comma, it will be cut.

If you need a given accurracy, you can use this function

#include <wx/numformatter.h>

double doubleNumber = 12.2345912375;
int accurracy = 10;
wxString numberString = wxNumberFormatter::ToString(doubleNumber, accurracy);

How to convert a wxString to a floating point number?

wxString number(wxT(3.14159));
double value;
if(!number.ToDouble(&value)){ /* error! */ }

so the string number is written to value.

I hope this is helpfull to someone, as everytime I wanted to convert something I started searching the web again. If there are improvements to make or I forgot something, feel free to correct me, as this is my first Q&A 🙂

All these questions and many more are answered in the wxWidgets wiki


c++ – How to work with wxString, numbers and other string types in wxWidgets

Leave a Reply

Your email address will not be published. Required fields are marked *