繁体   English   中英

处理 C 和 NCURSES 中的 Unicode 字符

[英]Handling Unicode characters in C and NCURSES

我正在尝试在 C 程序中显示一些 unicode 字符。 一个工作的 MWE 可以在下面看到:

#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <locale.h>


int main(int argc, char *argv[]) 
{ 
    setlocale(LC_ALL, "");
    initscr();              // Initialize stdscr

    for(int x = 0; x < 20; x++)
    {
        switch (x%5)
        {
            case 0:
                mvaddstr(1, x, "\u2588");
                break;
            case 1:
                mvaddstr(1, x, "\u2593");
                break;
            case 2:
                mvaddstr(1, x, "\u2592");
                break;
            case 3:
                mvaddstr(1, x, "\u2591");
                break;
            case 4:
                mvaddstr(1, x, " ");
                break;
        }
    }

    mvprintw(3, 0, "Press ANY KEY to finish");
    refresh();
    int ch = getch();
    endwin();

    return 0;
}

编译使用gcc -o shades shades.c -lncursesw 它编译良好并正确显示阴影,如下图所示。

色调

但相反,我想使用case/switch语句将我的字符放入一个十六进制代码数组中并对其进行迭代。 作为下面的可耻尝试。

#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

int main(int argc, char *argv[]) 
{ 
    setlocale(LC_ALL, "");
    initscr();              // Initialize stdscr

    uint shades[5] = { 0x2588,
                       0x2593,
                       0x2592,
                       0x2591,
                       ' '};

    char utfchar[7];

    for(int x = 0; x < 20; x++)
    {
        sprintf(utfchar, "\\u%04x", shades[x%5]);
        mvaddstr(1, x, utfchar);
    }

    mvprintw(3, 0, "Press ANY KEY to finish");
    refresh();

    int ch = getch();
    endwin();

    return 0;
}

在这里,我使用sprintf将十六进制值转换为格式为\的字符串,其中0000是正确的十六进制值。 然后我像在前面的代码中一样使用mvaddstr ,因为mvaddstr在第三个参数中需要一个const char *

这是众多失败的尝试之一。 我无法以 unicode 格式正确复制字符串,也无法在尝试添加 unicode 内容时使用变量作为mvaddstr参数。

我想知道如何从uint有效的 unicode 十六进制值格式化支持 unicode 的const char *以将其插入到mvaddstr

PS:我不是在 Linux 中使用 C++ 只是普通的 C。 C++ 解决方案不是解决方案

您可以简单地将字符串放入数组中:

const char *shades[] = { "\u2588",
                         "\u2593",
                         "\u2592",
                         "\u2591",
                         " "};

for(int x = 0; x < 20; x++)
{
    mvaddstr(1, x, shades[x%4]);
}

如果你想用代码点来做它,你需要将它编码为 UTF8(或任何 NCurse 期望的):

void sprintutf8(char *buffer, uint32_t code)
{
    if (code < 0x80)
        sprintf(buffer, "%c", code);
    else if (code < 0x800)
        sprintf(buffer, "%c%c",
            0xC0 | (code >> 6),
            0x80 | (code & 0x3F));
    else
        sprintf(buffer, "%c%c%c",
            0xE0 | (code >> 12),
            0x80 | (code >> 6 & 0x3F),
            0x80 | (code & 0x3F));
}

[...]

for(int x = 0; x < 20; x++)
{
    sprintutf8(utfchar, shades[x%4]);
    mvaddstr(1, x, utfchar);
}

您可以简单地使用wctomb cast 和wchar_t将十六进制转换为 unicode:

uint shades[5] = { 0x2588,
                   0x2593,
                   0x2592,
                   0x2591,
                   ' '};

char utfchar[MB_CUR_MAX];

for(int x = 0; x < 20; x++)
{
    memset(utfchar, 0, sizeof utfchar);
    wctomb(utfchar, (wchar_t)shades[x % 5]);
    mvaddstr(1, x, utfchar);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM