参考:
二、简介-
char :
c++中的关键字,占用1个字节。 -
wchar_t
https://www.educba.com/c-plus-plus-wchar_t/ -
char16_t、char32_t
https://en.cppreference.com/w/c/string/multibyte/char16_t -
char8_t:c++20标准中新增加的关键字。
https://open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0482r6.html
#includeusing namespace std; int main() { //char* loc = setlocale(LC_ALL,".65001"); char ch[] = "我"; char8_t ch8[] = u8"我"; wchar_t wch[] = L"我"; char16_t ch16[] = u"我"; char32_t ch32[] = U"我"; cout << &ch< 输出:
00B7F83C
00B7F830
00B7F824
00B7F818
00B7F808
ch:0xffffffe60xffffff880xffffff910
ch8:0xffffffe60x880x910
wch:0x6211
ch16:0x6211
ch32:0x6211
utf-8的中文是一个汉字占三个字节长度吗?
四、分析五、c++20标准:char8_t不可以输出
- char和char8_t中存的数字为:e6 88 91。代表汉字我的字符编码,三个字节,计算方式参考字符集vs字符编码。安装文章中的计算方式,换算后也是等于62 11.
- wchar_t、char16_t、char_32t中存的数字为:62 11。其中6211为Unicode的汉字”我"的字符编码;参见汉字的宽字符码和多字节码分别是多少
Recap
char8_t can not hold a full range of utf-8 glyphs. utf-8 glyphs can be up to 4 bytes. char8_t holds up to 1 byte.
char8_t stream out is expressly forbidden in C++20
for utf-8 compatibility while in VStudio use char and the /utf-8 switch
in case you really want C++20 way of transforming to/from char8_t, you need to use … AFAIK not yet fully implemented in any of the 3, as required by the standard参见:C++20 char8_t, the Boson



