如何使用带有ncurses的UTF-8重音符号

时间:2016-07-29 10:19:09

标签: c++ utf-8 ncurses

我最近安装了新的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 {1}}? 我使用ncursesw,这是我的g++命令来编译:

g++ file.cxx -o file -lncursesw

我的语言环境配置为fr_FR.UTF-8。 谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

getch()调用返回32位chtype值中的8位字符(char将无效,但在这种情况下不是问题)。 UTF-8是字节编码,在这种情况下,ncurses将返回每个调用一个字节。

要获取Unicode(宽字符)值,请使用get_wch,例如

wchar_t ch;
get_wch(&ch);

您当然应该检查get_wch的返回值,但其返回值为 OK ERR - 通过作为参数传递的指针返回该字符。

相关问题