目录
- 1. 文本
- 2. char* 转 wxString
- 3. wxString 转 char*
- 4. wchar_t* 转 wxString
- 5. wxString 转 wchar_t*
- 6. wxString 转 TCHAR
- 7. int 转 wxString
- 8. float 转 wxString
- 9. wxString 转 integer number
- 10. wxString 转 floating-point number
- 11. std::string 转 wxString
- 12. wxString 转 std::string
- 13. std::wstring 转 wxString
- 14, wxString 转 std::wstring
附加(数据库ADO类型):
文本
A literal is a string written in code with "quotes around it". A literal is not a wxString, and (in wxWidgets 2.8) will not be implicitly converted to one. This means that you can never pass in a raw literal into a wxWidget function or method (unless you don't care about your app not building with Unicode-enabled wxWidgets builds)
文本是一个在代码中被引号包围的串,文本它不是一个单纯的wxString类型,并且(在wxWidgets 2.8中)不能被隐含的转换为一个wxString类型。这意味着你不能试图将光秃秃的将一段文本放到wxWidget函数或方法中通过编译(除非你不在意你的应用程序是需要在Unicode编码环境中通过编译的)。
1 |
MessageBox<span style="color: #478f47">(</span><span style="color: #ff0000">"I'm a mistake!"</span><span style="color: #478f47">)</span> <span style="color: #ff8000">// WRONG in WxWidgets 2.8 (OK in 2.9) </span> |
Instead, wxWidgets (prior to wxWidgets 2.9) requires you to use one of these macros to turn literals into wxString-compatible characters:
1 2 3 4 5 6 |
_<span style="color: #478f47">(</span><span style="color: #ff0000">"text that can be translated"</span><span style="color: #478f47">)</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"text that won't be translated"</span><span style="color: #478f47">)</span> _T<span style="color: #478f47">(</span><span style="color: #ff0000">"same as wxT"</span><span style="color: #478f47">)</span> <span style="color: #993333">char</span>* c = <span style="color: #ff0000">"sometext"</span>; <a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span>c<span style="color: #478f47">)</span> <span style="color: #ff8000">// WRONG, not a literal </span> |
Rather than being a nuisance, the _(), wxT(), and _T() macros take care of some unicode issues and help with internationalization.
char* to wxString
1 2 3 |
<span style="color: #993333">const</span> <span style="color: #993333">char</span>* chars = <span style="color: #ff0000">"Hello world"</span>; <span style="color: #ff8000">// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span>chars, wxConvUTF8<span style="color: #478f47">)</span>; |
wxString to char*
1 2 3 4 5 6 7 |
<span style="color: #993333">void</span> my_function<span style="color: #478f47">(</span><span style="color: #993333">const</span> <span style="color: #993333">char</span>* foo<span style="color: #478f47">)</span> <span style="color: #478f47">{</span> <span style="color: #478f47">}</span> ... <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"HelloWorld"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #ff8000">// you could give the encoding you want as a parameter to mb_str(), e.g. mb_str(wxConvUTF8)</span> my_function<span style="color: #478f47">(</span> mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> <span style="color: #478f47">)</span>; |
mb_str() 返回一个临时的指针,如果你需要通过函数得到更多的返回结果(就和上面的情况一样),你可以临时保存一下这个字符数据流:
1 2 3 4 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> s<span style="color: #478f47">(</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"some string"</span><span style="color: #478f47">)</span> <span style="color: #478f47">)</span>; wxCharBuffer buffer=s.<span style="color: #202020">ToUTF8</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>; foo<span style="color: #478f47">(</span> buffer.<span style="color: #202020">data</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> <span style="color: #478f47">)</span>; <span style="color: #ff8000">// data() returns const char *</span> bar<span style="color: #478f47">(</span> buffer.<span style="color: #202020">data</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>, strlen<span style="color: #478f47">(</span>buffer.<span style="color: #202020">data</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span> <span style="color: #478f47">)</span>; <span style="color: #ff8000">// in case you need the length of the data </span> |
当你真的需要将它复制为char*类型时:
1 2 3 4 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"HelloWorld"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #993333">char</span> cstring<span style="color: #478f47">[</span><span style="color: #8f478f">1024</span><span style="color: #478f47">]</span>; <span style="color: #ff8000">// assuming you want UTF-8, change the wxConv* parameter as needed</span> strncpy<span style="color: #478f47">(</span>cstring, <span style="color: #478f47">(</span><span style="color: #993333">const</span> <span style="color: #993333">char</span>*<span style="color: #478f47">)</span>mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span>wxConvUTF8<span style="color: #478f47">)</span>, <span style="color: #8f478f">1023</span><span style="color: #478f47">)</span>; |
你也可以用ToUTF8(), 因为你得到的编码比用mb_str()函数从const char*转换成char*更加清楚。
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"HelloWorld"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #478f47">(</span>const_cast<char*><span style="color: #478f47">(</span><span style="color: #478f47">(</span><span style="color: #993333">const</span> <span style="color: #993333">char</span>*<span style="color: #478f47">)</span>mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span> |
在可变参数的函数 (如printf)中用mb_str()函数将无效,但按以下的方法是有效的:
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"HelloWorld"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066">printf</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"%s"</span>,mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>.<span style="color: #202020">data</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; |
做为选择,使用Potential Unicode Pitfalls中推荐的方法:
1 |
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066">printf</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"%s"</span>, <span style="color: #478f47">(</span><span style="color: #993333">const</span> <span style="color: #993333">char</span>*<span style="color: #478f47">)</span>mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span> |
wchar_t* to wxString
1 2 |
<span style="color: #993333">const</span> <span style="color: #993333">wchar_t</span>* chars = L<span style="color: #ff0000">"Hello world"</span>; <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span>chars<span style="color: #478f47">)</span>; |
wxString to wchar_t*
请翻阅官方文档的以下方法:
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">wc_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">wchar_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> |
wxString to TCHAR
1 2 3 4 5 6 7 |
TCHAR tCharString<span style="color: #478f47">[</span><span style="color: #8f478f">255</span><span style="color: #478f47">]</span>; <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> myString<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#underscoret"><span>_T</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"Hello World"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #993333">const</span> wxChar* myStringChars = myString.<span style="color: #202020">c_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>; <span style="color: #7c7c00">for</span> <span style="color: #478f47">(</span><span style="color: #993333">int</span> i = <span style="color: #8f478f">0</span>; i < myString.<span style="color: #202020">Len</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>; i++<span style="color: #478f47">)</span> <span style="color: #478f47">{</span> tCharString<span style="color: #478f47">[</span>i<span style="color: #478f47">]</span> = myStringChars <span style="color: #478f47">[</span>i<span style="color: #478f47">]</span>; <span style="color: #478f47">}</span> tCharString<span style="color: #478f47">[</span>myString.<span style="color: #202020">Len</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">]</span> = <a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#underscoret"><span>_T</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">'<span style="color: #000099; font-weight: bold">\0</span>'</span><span style="color: #478f47">)</span>; |
int to wxString
1 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring = <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">Format</span><span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"%i"</span><span style="color: #478f47">)</span>,myint<span style="color: #478f47">)</span>; |
或者
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring; mystring << myint; |
float to wxString
1 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring = <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">Format</span><span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"%f"</span><span style="color: #478f47">)</span>, myfloat<span style="color: #478f47">)</span>; |
或者
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring; mystring << myfloat; |
wxString to integer number
1 2 3 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> number<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"145"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #993333">long</span> value; <span style="color: #7c7c00">if</span><span style="color: #478f47">(</span>!number.<span style="color: #202020">ToLong</span><span style="color: #478f47">(</span>&value<span style="color: #478f47">)</span><span style="color: #478f47">)</span> <span style="color: #478f47">{</span> <span style="color: #ff8000">/* error! */</span> <span style="color: #478f47">}</span> |
或者
1 2 3 4 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> str = <a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#underscoret"><span>_T</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"123"</span><span style="color: #478f47">)</span>; <span style="color: #993333">int</span> num; num = wxAtoi<span style="color: #478f47">(</span>str<span style="color: #478f47">)</span>; |
wxString to floating-point number
1 2 3 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> number<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"3.14159"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; <span style="color: #993333">double</span> value; <span style="color: #7c7c00">if</span><span style="color: #478f47">(</span>!number.<span style="color: #202020">ToDouble</span><span style="color: #478f47">(</span>&value<span style="color: #478f47">)</span><span style="color: #478f47">)</span><span style="color: #478f47">{</span> <span style="color: #ff8000">/* error! */</span> <span style="color: #478f47">}</span> |
std::string to wxString
1 2 3 |
std::<span style="color: #993333">string</span> stlstring = <span style="color: #ff0000">"Hello world"</span>; <span style="color: #ff8000">// assuming your string is encoded as UTF-8, change the wxConv* parameter as needed</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span>stlstring.<span style="color: #202020">c_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span>, wxConvUTF8<span style="color: #478f47">)</span>; |
从wxWidgets 2.9开始, 你可以用适当的构造函数:
1 2 3 |
std::<span style="color: #993333">string</span> stlstring = <span style="color: #ff0000">"Hello world"</span>; <span style="color: #ff8000">// assuming your string is encoded as the current locale encoding (wxConvLibc)</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span>stlstring<span style="color: #478f47">)</span>; |
wxString to std::string
在wxWidgets 2.8 :
1 2 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span><a href="http://www.wxwidgets.org/manuals/stable/wx_stringfunctions.html#wxt"><span>wxT</span></a><span style="color: #478f47">(</span><span style="color: #ff0000">"HelloWorld"</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; std::<span style="color: #993333">string</span> stlstring = std::<span style="color: #993333">string</span><span style="color: #478f47">(</span>mystring.<span style="color: #202020">mb_str</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span><span style="color: #478f47">)</span>; |
在wxWidgets 2.9, 你可以用这个方法
1 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">ToStdString</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> |
std::wstring to wxString
从wxWidgets 2.9开始, 你可以用适当的构造函数:
1 2 3 |
std::<span style="color: #202020">sstring</span> stlstring = L<span style="color: #ff0000">"Hello world"</span>; <span style="color: #ff8000">// assuming your string is encoded as the current locale encoding (wxConvLibc)</span> <a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a> mystring<span style="color: #478f47">(</span>stlstring<span style="color: #478f47">)</span>; |
wxString to std::wstring
在wxWidgets 2.9, 你可以用这个方法
1 |
<a href="http://www.wxwidgets.org/manuals/stable/wx_wxstring.html"><span>wxString</span></a>::<span style="color: #202020">ToStdWstring</span><span style="color: #478f47">(</span><span style="color: #478f47">)</span> |
附:(数据库类型)
1 2 |
<span style="color:#0000ff;">wxString</span> str(wxT("Hello")); <span style="color:#0000ff;">_bstr_t</span> mystring=_bstr_t(str.wc_str()); |
_bstr_ 转 wxString
1 2 |
<span style="color:#0000ff;">_bstr_t</span> bstr="hello"; <span style="color:#0000ff;">wxString</span> mystring = <span style="color:#0000ff;">wxString</span>(static_cast<<span style="color:#0000ff;">const wchar_t</span> *>(bstr)); |
1 2 |
<span style="color:#0000ff;">_variant_t</span> varstr=<span style="color:#0000ff;">_variant_t</span>("Hello"); <span style="color:#0000ff;">wxString</span> mystring=<span style="color:#0000ff;">wxString</span>(static_cast<<span style="color:#0000ff;">const wchar_t</span> *>(<span style="color:#0000ff;">_bstr_t</span>(varstr))); |
1 2 |
<span style="color:#0000ff;">wxString</span> str(wxT("Hello")); <span style="color:#0000ff;">_variant_t</span> myvar=<span style="color:#0000ff;">_variant_t</span>(str.wc_str()); |
本文翻译自wxWidgets官方:http://wiki.wxwidgets.org/Converting_everything_to_and_from_wxString
除特别声明为转载内容外,本站所有内容均为作者原创,谢绝任何单位和个人不经许可的复制和转播!
对于确有转载需要的,请先与作者联系,在获得允许后烦请在转载时保留文章出处。
本文出自Lupin's Blog:http://www.cnzui.com/archives/290