当前位置:网站首页>c语言char, wchar_t, char16_t, char32_t和字符集的关系
c语言char, wchar_t, char16_t, char32_t和字符集的关系
2022-07-06 13:08:00 【landian004】
1,char不是标准里写死的固定8位,但它是固定的c/c++中的1 字节!
但一般就是8位。而且是8位时,还分 有符号和无符号,无符号时是0-255,有符号时是0-127。有符号时对应 的就只能是ASCII字符集了,因为iso-8859-1或windows1252或EBCDIC是256个字符的。我们的电脑上一般就是 有符号的,也就是对应的ASCII字符集。win,mac和linux上可以去验证(怎样验证?)
2,wchar_t是16位或32位(linux里是32位,win里是16位),所以它不可移植。
wchar_t还跟setlocale函数有联系,必须用到setlocale函数,它对应的字符集不知道跟setlocale有怎样的联系,第二,setlocale不知道跟char16_t 和char32_t有关联吗?总的来说,因为不可移植,用不到wchar_t,而直接用固定宽度字符集的char16_t和char32_t。
3,char16_t是确定的16位,也是确定的utf16字符集。而ucs-2才是确定的16位,utf16不是确定的16位,它是变长的(可以是16位和32位)。char16_t是能存储所有的utf16的code units,而不是code points(也即是所有字符),在utf16编码方案里超过16位长的字符用2个code units来表示。也即是utf16里超过16位长的字符是2个char16_t 来表示。
4,char32_t是确定的32位,确定的utf32 。只是缺点是它浪费空间!
5,但是实际使用中,不能用char16_t , char32_t,原因是语言和标准库里竟然没有 这2种c11新类型的输出方法!!所以还是只能用wchar_t!!
char* str="中文"; // 不对
wchar_t* str2 = L"中文"; //用这种
char16_t str3 = u"中文"; //没有wprintf等输出函数!所以不能用!
6,用char* str = "中文"; printf("%s", str); 同样能正确打印,但用strlen()函数测长度就错了,不应该用char* 或 const char* 或 char str[] 来表示中文字符串。问题:char* str和printf(而不是wprintf)也能正确表示和打印中文字符串的原因是什么呢?
7,char和wchar_t的表示和打印 都只是控制台程序才用到,当写图形界面程序时相应库里有中文在界面中的输出的函数,比如SDL里?但是控制台程序里表示和打印中文时,虽然不用wchar_t和wprintf和setlocale()函数仍然能正确处理,但正确做法仍然是用wchar_t,setlocale(),wprintf这些来表示。
8,setlocale(LC_ALL, "zh-CN"); // "zh-CN"或“zh-CN.UTF-8"或 "",这3种表示法都行,为了可移植推荐用"zh-CN" 这种写法(实际试验得 "zh-CN.UTF-8"也行,且不区分大小写)。"zh-CN"这里还有更多表示法。但是setlocale的原理仍不清楚??
以上是查阅阶段1的总结。
边栏推荐
- Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
- 面试官:Redis中有序集合的内部实现方式是什么?
- Binary tree node at the longest distance
- document. Usage of write () - write text - modify style and position control
- 分糖果
- What is the problem with the SQL group by statement
- Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces
- Proxy and reverse proxy
- MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
- OSPF multi zone configuration
猜你喜欢
3D人脸重建:从基础知识到识别/重建方法!
[redis design and implementation] part I: summary of redis data structure and objects
OneNote 深度评测:使用资源、插件、模版
Aike AI frontier promotion (7.6)
Aiko ai Frontier promotion (7.6)
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Set up a time server
[interpretation of the paper] machine learning technology for Cataract Classification / classification
随机推荐
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
Aike AI frontier promotion (7.6)
Ravendb starts -- document metadata
Why do job hopping take more than promotion?
Technology sharing | packet capturing analysis TCP protocol
R3live notes: image processing section
Swagger UI tutorial API document artifact
Common English vocabulary that every programmer must master (recommended Collection)
c#使用oracle存储过程获取结果集实例
039. (2.8) thoughts in the ward
Aiko ai Frontier promotion (7.6)
[interpretation of the paper] machine learning technology for Cataract Classification / classification
Reference frame generation based on deep learning
爱可可AI前沿推介(7.6)
Absolute primes (C language)
Math symbols in lists
JS get array subscript through array content
Nodejs tutorial expressjs article quick start
Caching strategies overview
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug