开发环境:VS2008
开发语言:C++
#include <iostream>
#include <windows.h>
#include <string>
int main(int argc, char *argv)
{
/**
* wchar_t 定义的变量为什么不能输出呢?
* 宽字符类型要本地化,否则输不出想要结果。
* 本地化有三条语句可以使用,任取其一。最后一句是全局函数,前两个是wcout的一个成员函数的两种表达方式。
*/
//std::wcout.imbue(std::locale("chs"));
//std::wcout.imbue(std::locale(""));
setlocale(LC_ALL,"Chinese-simplified");
// 宽字符转多字节
wchar_t wstr[100] = L"wchar_t中国";
char str[100];
wcstombs(str,wstr,100); //宽字符转多字节
printf("char: %s\r\n",str);
// 多字节转宽字符
wchar_t wstr1[100];
char str1[100] = "hello word 中国!";
mbstowcs(wstr1, str1, 100); // 多字节转宽字符
// 输出 wchar_t 字符串是用 wprintf 或 wcout
wprintf(wstr1);
std::wcout << "wchar: " << wstr1 << std::endl;
system("PAUSE");
}