Print Unicode characters in C, using ncurses

混江龙づ霸主 提交于 2019-12-01 06:25:06

问题


I have to draw a box in C, using ncurses;

First, I have defined some values for simplicity:

#define RB "\e(0\x6a\e(B"  (ASCII 188,Right bottom, for example)

I have compiled with gcc, over Ubuntu, with -finput-charset=UTF-8 flag.

But, if I try to print with addstr or printw, I get the hexa code. What I`m doing wrong?


回答1:


ncurses defines the values ACS_HLINE, ACS_VLINE, ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER and ACS_LRCORNER. You can use those constants in addch and friends, which should result in your seeing the expected box characters. (There's lots more ACS characters; you'll find a complete list in man addch.)

ncurses needs to know what it is drawing because it needs to know exactly where the cursor is all the time. Outputting console control sequences is not a good idea; if ncurses knows how to handle the sequence, it has its own abstraction for the feature and you should use that abstraction. The ACS ("alternate character set") defines are one of those abstractions.




回答2:


A few issues:

  • if your program writes something like "\e(0\x6a\e(B" using addstr, then ncurses (any curses implementation) will translate the individual characters to printable form as described in the addch manual page.
  • ncurses supports line-drawing for commonly-used pseudo-graphics using symbols (such as ACS_HLINE) which are predefined characters with the A_ALTCHARSET attribute combined. You can read about those in the Line Graphics section of the addch manual page.
  • the code 0x6a is ASCII j, which (given a VT100-style mapping) would be the lower left corner. The curses symbol for that is ACS_LRCORNER.
  • you cannot write the line-drawing characters with addstr; instead addch, addchstr are useful. There are also functions oriented to line-drawing (see box and friends).
  • running in Ubuntu, your locale encoding is probably UTF-8. To make your program work properly, it should initialize the locale as described in the Initialization section of the ncurses manual page. In particular:

    setlocale(LC_ALL, "");

    Also, your program should link against the ncursesw library (-lncursesw) to use UTF-8, rather than just ncurses (-lncurses).

  • when compiling on Ubuntu, to use the proper header definitions, you should define _GNU_SOURCE.


来源:https://stackoverflow.com/questions/34538814/print-unicode-characters-in-c-using-ncurses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!