我正在重写这个问题。
我想使用整数常量创建一个带有 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 ,但这听起来很有希望。
- How to convert a Unicode code point to characters in C++ using ICU? :可能是您问题的答案
- Unicode Processing in C++ :一个相关的 Stack Overflow 问题
关于c++ - printf 从整数转义 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788328/