我要在 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;
}
现在的问题是:
although string2 is showing the corect output in terminal, string1 isn't.
But I must use the 1st approach, i.e I have the unicode characters stored in some arbitrary variables & I need a way to get them printed on screen.
如有任何建议,我们将不胜感激。
最佳答案
为了打印存储在变量中的 Unicode 字符,为什么不将它们转换为 wchar_t
并使用例如wprintf
?
wchar_t ac = a;
wchar_t bc = b;
wprintf(L"%c%c", ac, bc);
关于c - 在 C 中打印 Unicode 字符(存储在变量中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017342/