一、wxString与其他类型转换
1.转换为标准字符串(std::string或char*):
wxString wxStr = wxT("Hello, 中文!");
std::string stdStr = wxStr.ToStdString(wxMBConvUTF8()); // 返回字符串的编码由wxMBConv对象指定,可能转换失败
const char* cStr = wxStr.c_str(); // 转换为当前语言环境编码的字符串,转换可能失败
std::string stdStr = std::string(cStr);
const char* mbStr = wxStr.mb_str(wxMBConvUTF8()); // 始终编译,显式编码,但是转换可能失败!
std::string stdStr = std::string(mbStr);
const char* utf8Str = wxStr.ToUTF8(); // 始终编译,编码永远不会失败
// const char* utf8Str = wxStr.utf8_str(); // 上面的别名
std::string stdStr = utf8Str;
2.标准字符串(std::string或char*)转换为wxstring
std::string stdStr = "hello, 中文!";
wxString wxstr=wxString(stlString.c_str(),wxConvUTF8);//显式编码 UTF8编码
//
wxString wxstr=wxString::FromUTF8(stdStr); // UTF8编码
const char* cStr="hello, 中文!";
wxString wxstr=wxString(cStr, wxConvUTF8);//显式编码 UTF8编码
//
wxString wxstr=wxString::FromUTF8(cStr); // UTF8编码
3.转换为整数或浮点数:
wxString wxNumStr = wxT("123");
int intNum = wxStrtol(wxNumStr, NULL, 10); // 转换为int
//或
int i = wxAtoi(wxNumStr);
//或
int i;
wxNumStr.ToInt(&i);
//或
long l = wxAtol(wxNumStr);
//或
long l;
wxNumStr.ToLong(&l);
...
double doubleNum = wxStrtod(wxNumStr, NULL); // 转换为double
//或
double d;
wxNumStr.ToDouble(&d);
//或
double dd= wxAtof(wxNumStr);
4.整数或浮点数转换为wxString:
int intNum = 123;
wxString wxNumStr = wxString::Format("%d", intNum); // 从int转换
double doubleNum = 123.456;
wxString wxNumStr = wxString::Format("%.3f", doubleNum); // 从double转换