如何使用新的 char16_t 和 u8 类型? [英] How to use the new char16_t and u8 types?
问题描述
c11 使用 utf-8 utf-16 和 utf-32 的内置类型改进了编码支持.
c11 improve encoding support with built-in types for utf-8 utf-16 and utf-32.
但是我完全找不到在标准函数中使用它们的参考.我发现的是如何在 c++11 中而不是在 C 中使用它们.
However I was completely unable to find reference on using them in Standard functions. All I found is how to use them in c++11 not in C.
例如,如何打印一个 char32_t?
So how to printf a char32_t for example?
推荐答案
没什么好说的:C11 只引入了四个新的标准库函数,用于处理 char16_t
和 char32_t
,将它们转换为/从多字节字符串:
There isn't much to say: C11 only introduced four new standard library functions for working with char16_t
and char32_t
, which convert them to/from multibyte strings:
char16_t
:mbrtoc16()
和c16rtomb()
char32_t
:mbrtoc32()
和c32rtomb()
对于 printf()
,它们的行为类似于 uint_least16_t
和 uint_least32_t
类型,因此您可以对它们使用相同的格式说明符:
With respect to printf()
, they behave like uint_least16_t
and uint_least32_t
types, so you can use the same format specifiers for them:
#include <inttypes.h>
char32_t x = ...;
printf("%" PRIuLEAST32 "\n", x);
如果要将值打印为字符,则需要使用上面的转换函数.
If you want to print the value as a character, you will need to use the conversion functions above.
使用 char16_t
和 char32_t
字符和字符串文字是相同的在 C11 和 C++11 中.
Working with char16_t
and char32_t
character and string literals is identical
in both C11 and C++11.
这篇关于如何使用新的 char16_t 和 u8 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!