【发布时间】:2012-08-14 14:00:52
【问题描述】:
我要在 C 中打印一些 unicode(4 位十六进制)字符。
我存储在一些 short int 变量中的那些字符。以下是我应该用于我的目的的代码:
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
int main(void) {
setlocale(LC_ALL,"");
short a = 0x099A, b = 0x09BE;
wchar_t *string1 = (wchar_t *) calloc(20, sizeof(wchar_t));
sprintf(string1, "\\u%04x\\u%04x", a, b);
printf(" %s ", string1);
wchar_t *string2 = (wchar_t *) calloc(20, sizeof(wchar_t));
strcpy(string2, (wchar_t *) "\u099A\u09BE");
printf(" %s \n", string2);
return 0;
}
现在的问题是:
虽然 string2 在终端中显示正确的输出,但 string1 不是。
但我必须使用 1st 方法,即我将 unicode 字符存储在一些任意变量中,我需要一种方法来获取它们打印在屏幕上。
任何建议都将不胜感激。
【问题讨论】:
-
可移植性说明:不保证
wchar_t是 4 字节长或可以存储 Unicode 代码点。