使用ncurses在C中打印Unicode字符 [英] Print Unicode characters in C, using ncurses
问题描述
首先,为了简单起见,我定义了一些值:
#define RB\e(0 \x6a\e(B(例如ASCII 188,右下)
我使用gcc编译了Ubuntu,并使用 -finput-charset = UTF-8
flag。
但是,如果我尝试使用addstr或printw进行打印,我会得到hexa代码
我在做什么错了?
有几个问题: 在Ubuntu中运行时,您的区域设置编码可能是UTF-8。为了使程序正常工作,它应该按照初始化部分。特别是: setlocale(LC_ALL,); 另外,你的程序应该链接到ncurses (
\e(0x6a)(B
使用 addstr ,那么ncurses(任何 curses实现)都会将单个字符转换为可打印格式,如 addch 手册页。
ACS_HLINE
)通常使用的伪图形的线图,这些符号是 A_ALTCHARSET
属性合并。您可以阅读有关 Line Graphics < addch
手册页。
0x6a
是ASCII j
,其中(给定VT100风格的映射)将是左下角。这个curses符号是 ACS_LRCORNER
。
addstr
;而不是
addch
, addchstr
很有用。还有一些面向线画的功能(请参见 box
和朋友)。
-lncursesw
)使用UTF-8,在Ubuntu上编译时,为了使用正确的头文件定义,你应该定义 _GNU_SOURCE
。
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?
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 theA_ALTCHARSET
attribute combined. You can read about those in the Line Graphics section of theaddch
manual page. - the code
0x6a
is ASCIIj
, which (given a VT100-style mapping) would be the lower left corner. The curses symbol for that isACS_LRCORNER
. - you cannot write the line-drawing characters with
addstr
; insteadaddch
,addchstr
are useful. There are also functions oriented to line-drawing (seebox
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
.
这篇关于使用ncurses在C中打印Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!