wxString char* int float 之间的转换

1、char* to wxString

const char* chars = "Hello world";
// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed
wxString mystring(chars, wxConvUTF8);


2、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())



3、wchar_t* to wxString

const wchar_t* chars = L"Hello world";
wxString mystring(chars);



4、wxString to wchar_t*

See the following methods in the docs :
wxString::wc_str() 
wxString::wchar_str()


5、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');


6、int to wxString

wxString mystring = wxString::Format(wxT("%i"),myint);
or
wxString mystring;
mystring << myint;



7、float to wxString


wxString mystring = wxString::Format(wxT("%f"), myfloat);
or
wxString mystring;
mystring << myfloat;


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


9、wxString to floating-point number

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


10、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 2.9, 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);



11、wxString to std::string

wxWidgets 2.8 :
wxString mystring(wxT("HelloWorld"));
std::string stlstring = std::string(mystring.mb_str());
Under wxWidgets 2.9, you may use

wxString::ToStdString()


12、std::wstring to wxString

Starting from wxWidgets 2.9, you may use the appropriate constructor
std::sstring stlstring = L"Hello world";
// assuming your string is encoded as the current locale encoding (wxConvLibc)
wxString mystring(stlstring);



13、wxString to std::wstring

Under wxWidgets 2.9, you may use
wxString::ToStdWstring()



14、char*->TCHAR*

char *ansii_string = "some text";
int size = strlen(ansii_string);
TCHAR unicode_string[30];
mbstowcs(unicode_string, ansii_string, size+1);



15、TCHAR*->char*

TCHAR* unicode_string=_T("unicode string");
int size= wcslen(unicode_string);
char ansi_string[30];
wcstombs(ansi_string, unicode_string, size+1);



16、wxString->char*

wxString wx_string=_T("wx string");
char ansi_string[30];

strcpy(ansi_string,wx_string.mb_str());


17、char*->wxString

char *ansii_string = "some text";

wxString wx_string(ansii_string,wxConvUTF8);


18、wxString->TCHAR*

wxString wx_string=_T("wx string");
TCHAR wchar_string[30];
wcscpy(wchar_string,wx_string.wc_str());



19、TCHAR*->wxString

TCHAR *tchar_string = _T("some text");
wxString wx_string(tchar_string,wxConvUTF8); 


20、char*->TCHAR*

char *ansii_string = "some text";
int size = strlen(ansii_string);
TCHAR unicode_string[30];
mbstowcs(unicode_string, ansii_string, size+1);


21、TCHAR*->char*

TCHAR* unicode_string=_T("unicode string");
int size= wcslen(unicode_string);
char ansi_string[30];
wcstombs(ansi_string, unicode_string, size+1);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值