Reputation: 435
As I startded programming in C++ with wxWidgets (in code::blocks), I often had the issue that I did not know how to use wxString in a good way. The main reason was that there are already several string types in C++ and wxWidgets now adds another. I already had some classes imported from another project without wxWidgets, so now everything needed to be compatible.
This lead to several questions:
Upvotes: 3
Views: 10618
Reputation: 435
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.
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
wxString::ToStdString()
And for std::wstring under wxWidgets 3.0, you may use
wxString myString = "abc";
std::wstring myWString = wxString::ToStdWstring(myString);
(not tested, documented here).
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 = L"Hello world"; // assuming your string is encoded as the current locale encoding (wxConvLibc) wxString mystring(stlstring);
You can either use
int number = 3;
wxString myString = wxString::Format(wxT("%i"), number);
or
#include <wx/numformatter.h>
int number = 3;
wxString myString = wxNumberFormatter::ToString(number);
The second method is documented here. You don't 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
).
I always use this method:
wxString numberString = "12345";
int number = wxAtoi(numberString);
You can use the first method if you don't 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);
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 :)
Upvotes: 5
Reputation: 20452
All these questions and many more are answered in the wxWidgets wiki
https://wiki.wxwidgets.org/Converting_everything_to_and_from_wxString
Upvotes: 0