wxWidgets 中类型转换

char* to wxString

char* chars = "Hello world";
wxString mystring(chars, wxConvUTF8);

 

wxString to char*

void my_function(const char* foo)
{
}
wxString mystring(wxT("HelloWorld"));
my_function( mystring.mb_str() );

   mb_str() returns a temporary pointer. If you need to store it in a char* :

wxString mystring(wxT("HelloWorld"));
char cstring[1024];
strcpy(cstring, (const char*)mystring.mb_str(wxConvUTF8));

   You can also use ToUTF8(), since which encoding you get is clearer than with mb_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(!s.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";
wxString mystring(stlstring.c_str(), wxConvUTF8);

 

wxString to std::string

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

posted on 2010-02-27 10:51  骑驴睡觉  阅读(807)  评论(0编辑  收藏  举报

导航