Unicode下CString转成const char* 的汉字乱码问题。

time_is_life 2014-08-24 04:33:28
在Unicode字符集下,有如下的代码:


USES_CONVERSION;
CString str2 = TEXT("我来了");
const char* convert1 = W2A(str2 );
const char* convert2 = T2A(str2 );


convert1和convert2都是???,
为什么?该如何解决?请指教。谢谢。
...全文
210 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
encoderlee 2014-08-24
  • 打赏
  • 举报
回复
用WideCharToMultiByte来转,第一个参数用936(简体中文) W2A和T2A默认是使用系统当前代码页,但是英文系统的默认代码页不是936简体中文,所以转换出错
time_is_life 2014-08-24
  • 打赏
  • 举报
回复
另外我加上如下的代码,str3和str4输出到界面上的的都是问号。

		CString str2 = TEXT("我来了");
		const char* convert1 = W2A(str2 );
		const char* convert2 = T2A(str2 );
		CString str4 = A2W( convert1 );
		CString str3(convert1);
		GetDlgItem( IDC_TEXT ) -> SetWindowText( str3 );
		GetDlgItem( IDC_TEXT ) -> SetWindowText( str4 );
但是这样直接写就能输出汉字

		GetDlgItem( IDC_TEXT ) -> SetWindowText( TEXT("汉字测试") );
这到底是为什么?
time_is_life 2014-08-24
  • 打赏
  • 举报
回复
我是在调试器里,是英文系统,有中文输入法。 该如何让当前字符集支持中文那?谢谢。
encoderlee 2014-08-24
  • 打赏
  • 举报
回复
阿呆_ 2014-08-24
  • 打赏
  • 举报
回复
unicode中文转MBCS中文的前提条件是当前字符集支持中文。 另外你从哪里看到convert1和convert2是???的? 别告诉说是在调试器里哦。
wince上各种类型转换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //转换类,所有的转换函数在这里静态 2是to的意思 //cs:cstring //s:string //i:int class cv { public: static char * s2charx(string s); //static char * cs2charx_unicode(CString s); static char * cs2charx_unicode(CString s); static CString charx2cs(char *s); cv(); virtual ~cv(); static int cstring_to_int(CString t); static CString s2cs(string text); static CString i2cs(int i); static string cs2s(CString s); static void Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static void Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static BOOL IsNumeric(string text); static void UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen); static void Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static int cs2i(CString s); static string int_to_string(int n); static CString ChineseCapitalMoney(double Num); //static wstring s2ws( string s ); static std::wstring s2ws(const string& s); static std::string WChar2Ansi(LPCWSTR pwszSrc); static string ws2s(wstring& inputws); static const char * s2constcharx(string s); static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen); static char * cs2charx_ansi(CString s); //static string ws2s(wstring ws); };

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧