我最近安装了新的 Windows 10 build 14393,我想使用新的 linux 子系统。所以我决定学习 ncurses,但我找不到如何从 getch
中获取带有重音符的字符的 UTF-8 代码,例如 è
。
所以这是我的代码:
#include <iostream>
#include <string>
#include <curses.h>
using namespace std;
int main(int argc, char **argv)
{
char ch;
setlocale(LC_ALL, "fr_FR.UTF-8");
initscr();
cbreak();
noecho();
printw("èèè\n"); // i can print accents
ch = getch(); // if i press è it gives me 2 characters
printw("%d", ch);
ch = getch();
printw("%d", ch);
getch(); // wait for a key to exit
endwin();
return 0;
}
例如,如果我按 è
,它会给我 2 个字符的代码,分别是 -61
和 -88
,那么我怎样才能得到它UTF-8
表中的代码是 232
?
我使用 ncursesw,这是我要编译的 g++
命令:
g++ file.cxx -o file -lncursesw
我的语言环境配置为 fr_FR.UTF-8。 感谢您的回答。
最佳答案
getch()
调用返回 32 位 chtype
值中的 8 位字符(char
不能正常工作,但可以在这种情况下不是问题)。 UTF-8 是一种多字节编码,在这种情况下,ncurses 将返回字节——每次调用一个字节。
要获得 Unicode(宽字符)值,请使用 get_wch
,例如,
wchar_t ch;
get_wch(&ch);
你当然应该检查 get_wch
的返回值,但它的返回值应该是 OK
或 ERR
— 通过作为参数传递的指针返回字符。
关于c++ - 如何使用 ncurses 获取 UTF-8 重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38656359/