【问题标题】:Printing Unicode Character (stored in variables) in C在 C 中打印 Unicode 字符(存储在变量中)
【发布时间】: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 代码点。

标签: c unicode


【解决方案1】:

为了打印存储在变量中的 Unicode 字符,为什么不将它们转换为 wchar_t 并使用例如wprintf?

wchar_t ac = a;
wchar_t bc = b;

wprintf(L"%c%c", ac, bc);

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2016-09-18
    • 1970-01-01
    • 2018-05-02
    • 2021-12-26
    相关资源
    最近更新 更多