赞
踩
备注:也有人称多字节字符为窄字符。但是《c++标准程序库》中称为多字节字符,我们沿用书中多字节的说法。
备注:也有文章中说,ASCII即无所谓宽字节和多字节。
ISO C++使用wchar_t表示宽字符,在c++中wchar_t已被扶正为关键词。
已经验证:
Class template codecvt用来在不同的字符编码方案之间进行转换
备注:未来再写
这个区分的文章不是太多,但是我们要明白。
这个简要再多说2点,后面博客会详细介绍
#include <iostream> #include <locale> void setLoc() { std::locale::global(std::locale("chs")); } int main() { using namespace std; //cout.imbue(locale("chs")); char ch1 = 'A'; // 正常 char ch2 = '中'; // 错误,一个char不能完整存放一个汉字信息 char ch3[4] = "中"; //前2个字节存放汉字'中',第3个字节存放字符串结束符\0 cout << ch1 <<endl; cout << ch2 << endl; cout << ch3 << endl << endl; setLoc(); char ch4 = 'A'; char ch5 = '中'; char ch6[4] = "中"; locale loc2; cout << ch4 << endl; cout << ch5 << endl; cout << ch6 << endl; system("pause"); return 0; }
输出:
#include <iostream> #include <locale> using namespace std; void setLoc() { std::locale::global(std::locale("chs")); } int main() { //wcout.imbue(locale("chs")); // 设置wcout //setlocale(LC_ALL, "chs"); // c语言设置locale为chs setLoc(); wchar_t ch1 = L'A'; // L代表宽字符 wchar_t ch2 = L'中'; // 一个汉字用一个wchar_t表示 wchar_t ch3[4] = L"中"; //前两个字节(前一个wchar_t)存放汉字'中',最后两个字节(后一个wchar_t)存放字符串结束符\0 wcout << ch1 <<endl; wcout << ch2 << endl; wcout << ch3 << endl << endl; system("pause"); return 0; }
输出
#include <iostream> #include <locale> using namespace std; int main() { char * loc=setlocale(LC_ALL, ""); // 或者设置为chs,都可以输出,utf8 ,.65001都可以输出 wchar_t ch1 = L'A'; // 正常 wchar_t ch2 = L'中'; // 错误,一个char不能完整存放一个汉字信息 wstring str = L"中"; //前两个字节(前一个wchar_t)存放汉字'中',最后两个字节(后一个wchar_t)存放字符串结束符\0 wcout << ch1 <<endl; wcout << ch2 << endl; wcout << str << endl << endl; system("pause"); return 0; }
输出为
A
中
中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。