[英]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.