c - 在 C 中打印 Unicode 字符(存储在变量中)

标签 c unicode

我要在 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/

相关文章:

c - 这在 C 语言中意味着什么?

c - 如何在 ZeroMQ 中以定义的速率发送消息?

c - 在一个数组中查找另一个数组

c - 除第一个之外,结构成员的值无法正确打印

Unicode 命名的文件夹显示?在 wscript 提示符下

delphi - Delphi 2010 中的 Indy IdHttp Post 问题

java - 使用 PDFBox 将 UTF-8 编码的字符串写入 PDF

php - 我找不到用于设置 "DOMPDF_UNICODE_ENABLED"true 的 dompdf_config.inc.php 或 dompdf_config.custom.inc.php

javascript - 如何可靠地去除破坏代码的不可见字符?

c++ - 如何使用 dlmopen 在不同线程中打开多个共享库?