当前位置:网站首页>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的总结。
边栏推荐
- 基于深度学习的参考帧生成
- OneNote in-depth evaluation: using resources, plug-ins, templates
- Sdl2 source analysis 7: performance (sdl_renderpresent())
- Data Lake (VIII): Iceberg data storage format
- [interpretation of the paper] machine learning technology for Cataract Classification / classification
- 038. (2.7) less anxiety
- Torch Cookbook
- Yyds dry inventory run kubeedge official example_ Counter demo counter
- Aiko ai Frontier promotion (7.6)
- Is it profitable to host an Olympic Games?
猜你喜欢

快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
![[Li Kou brush questions] 32 Longest valid bracket](/img/51/1ce4f9e8517dba214ec82b6567c923.png)
[Li Kou brush questions] 32 Longest valid bracket

966 minimum path sum

Seven original sins of embedded development

【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

OneNote in-depth evaluation: using resources, plug-ins, templates

Aiko ai Frontier promotion (7.6)

Aike AI frontier promotion (7.6)

基于深度学习的参考帧生成

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
随机推荐
038. (2.7) less anxiety
document. Usage of write () - write text - modify style and position control
Seven original sins of embedded development
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
【mysql】游标的基本使用
Math symbols in lists
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
R语言做文本挖掘 Part4文本分类
技术分享 | 抓包分析 TCP 协议
Is this the feeling of being spoiled by bytes?
20220211 failure - maximum amount of data supported by mongodb
JS学习笔记-OO创建怀疑的对象
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Set up a time server
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Nodejs教程之Expressjs一篇文章快速入门
分糖果