我的代码是:
#include <string>
#include <iostream>
#include <wx/string.h>
int main(int n, char** c) {
std::string a = "你好";
wxString b = a;
std::cerr <<a.length()<<"/"<< b.Len()<<"\n";
}
我希望结果像“6/6”,但我得到的是“6/0”。也就是说,b是空的。我做错了什么?
我尝试了其他转换,但它们也无效。
编译:
g++ `wx-config --cxxflags --libs` -std=c++11 -o string-test string-test.cpp
wx版本是3.0.0.0。 gcc版本4.9.2 20141101(Red Hat 4.9.2-1)(GCC)。
答案 0 :(得分:2)
wxString
使用当前语言环境来解释其输入,如果是程序,则此语言环境是默认的“C”,因为您永远不会更改它。将其设置为与源文件使用的编码相对应的区域设置,以允许代码按预期的方式工作。
两个更好的选择是:
std::wstring = L"..."
。wxString::FromUTF8("...")
。