c++ - printf 从整数转义 unicode 字符

标签 c++ unicode printf

我正在重写这个问题。

我想使用整数常量创建一个带有 unicode 转义字符的字符串,例如 "\u03B1"。例如,该字符串是希腊字母 alpha。

const char *alpha = "\u03B1"

我想使用整数值 0x03B1 调用 printf 来构造相同的字符串。对于此示例,可以这样做,但我不确定是否从 0x03B1 中获取这两个数字。

printf("%c%c", 206, 177);

此链接解释了要做什么,但我不知道该怎么做。 http://www.fileformat.info/info/unicode/utf8.htm

For characters equal to or below 2047 (hex 0x07FF), the UTF-8 representation is spread across two bytes. The first byte will have the two high bits set and the third bit clear (i.e. 0xC2 to 0xDF). The second byte will have the top bit set and the second bit clear (i.e. 0x80 to 0xBF).

注意:我不想创建带有反斜杠的字符串 "\\u03B1"。这与 "\u03B1" 不同,后者是转义的 unicode 字符。

最佳答案

看来即使是最新的 C 和 C++ 标准在处理 Unicode 方面也有点令人失望。

对于那些对问题中的示例感到困惑的人,就像我一样:

const char *alpha = "\u03B1"

在 C99 中,这将在 alpha 中存储指向字符串 "α" (U+03B1) 的指针。在 C89 中,这是无效语法。

我找不到将 \u 语法与变量或整数常量一起使用的方法,就像问题所要求的那样。您最好使用库来为您的程序添加更好的 Unicode 支持。我没有使用过ICU library ,但这听起来很有希望。

关于c++ - printf 从整数转义 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788328/

相关文章:

c - 对 printf 的嵌套调用

c++ - 如何特征默认构造函数是私有(private)的

c++ - 调用可执行文件的函数代码

c++ - DirectX11 仅在已填充的索引和顶点缓冲区中绘制一半的顶点

c++ - 循环范围 : VS 2008 doesnt support it?

java - 使用 itextpdf 将 .pptx 转换为 .pdf。汉字字符定位错误

c++ - 通过非 unicode 代码读取 UTF-8 Unicode 文件

c - 为什么在printf中指定为整数的char被正确打印

mysql - 如何在mysql中正确存储和显示unicode

c - sprintf 似乎正在四舍五入?