C++中TCHR转string

一般C++标准库中的string不支持宽字节,也就是unicode字符集。

所以要想将TCHAR或wchar转换成string,则需调用系统函数WideCharToMultiByte

int len = WideCharToMultiByte(CP_ACP, 0, *s, -1, NULL, 0, NULL, NULL);
char *new_str = new char[len * sizeof(char)]; WideCharToMultiByte(CP_ACP, 0, *s, -1, new_str, len, NULL, NULL);
string s(new_str);

反之,要想将多字节转换为unicode,则需使用相反的函数即可MultiByteToWideChar

若工程的字符集为unicode,则可以使用标准库的宽字符版,即名字前加w,例如wstring,wcout,wcin

posted @ 2016-03-11 16:34  Travis_007  阅读(1013)  评论(0编辑  收藏  举报