在 ncurses 中从键盘读取 UTF-8 编码的字符
Read UTF-8 encoded character from keyboard in ncurses
在 nCurses 中读取键盘输入时,我使用 getch() 函数,该函数对 ASCII 字符工作正常,但对 UTF-8 编码字符无效。如果我在键盘上按下字符 ś:
int c = getch();
c 的值应为十六进制:0xC59B。但是当我尝试打印它的值时,我只得到 0xC5。
如何读取整个字符以及 getch() 使用的函数是否正确?
getch
的名字来源于旧地球"character"的意思,实际上就是"byte"。此机制无法理解多字节编码。
然而,; you just need to deal with its result properly. Call it repeatedly and dump what you get into a string of bytes (in your particular example, you'll need two calls to obtain enough bytes to represent the particular Unicode character provided), then interpret those bytes with a UTF-8 library.
不要忘记过滤掉 getch
可以提供的 "special values",如 it does not always give you raw characters(例如,考虑 F1 键!).
getch
读取字节,但是UTF-8是多字节的。您可以逐字节读取并解释它,但这是大多数人不需要的工作。使用 get_wch
读取(整个) 宽字符 .
假定您initialized ncurses 的语言环境:
setlocale(LC_ALL, "");
(如果你不这样做,getch
无论如何都不会 return 正确的字节)。
在 nCurses 中读取键盘输入时,我使用 getch() 函数,该函数对 ASCII 字符工作正常,但对 UTF-8 编码字符无效。如果我在键盘上按下字符 ś:
int c = getch();
c 的值应为十六进制:0xC59B。但是当我尝试打印它的值时,我只得到 0xC5。
如何读取整个字符以及 getch() 使用的函数是否正确?
getch
的名字来源于旧地球"character"的意思,实际上就是"byte"。此机制无法理解多字节编码。
然而,
不要忘记过滤掉 getch
可以提供的 "special values",如 it does not always give you raw characters(例如,考虑 F1 键!).
getch
读取字节,但是UTF-8是多字节的。您可以逐字节读取并解释它,但这是大多数人不需要的工作。使用 get_wch
读取(整个) 宽字符 .
假定您initialized ncurses 的语言环境:
setlocale(LC_ALL, "");
(如果你不这样做,getch
无论如何都不会 return 正确的字节)。