Skip to main content
GameDev.net gamedev.net
🔒 Locked

NCurses and UTF-8 :(

Started by Daggerbot Apr 6, 2011 at 9:01 PM 1 replies 5.6k views
Original Post
Daggerbot
Daggerbot
I'm using NCurses for the Unix-like port of a text-based app. I know for a fact that my terminal (xfce4-terminal) supports UTF-8, as I have tested it with printf. Afaik, most other major terminals, such as the ones used by Gnome, KDE, and Mac OS X, support UTF-8 as well. I expected that printing UTF-8 text to NCurses would be pretty straightforward, but I was apparently wrong. In my test program which draws a box with Unicode box drawing chars, the output is junk. Interestingly enough, the junk consisted only of ASCII chars. This is strange because the strings don't contain any ASCII chars at all, with the exception of spaces. It appears that NCurses is discarding the highest bit of each byte. Why would it do this? Is there some kind of function that enables UTF-8 output?

If all else fails, I'll change my console class to print pure ANSI escape sequences, rather than using NCurses. I would have done this in the first place if I had any idea to handle input from non-printable keys like F1-F12, arrows, page up/down, etc.

Has anyone else here had the same problem with NCurses? Any help is appreciated.

Daggerbot
Daggerbot
I've found a partial solution. Linking to -lncursesw rather than -lcurses and setting the locale to "en_US.UTF-8" makes the box chars appear correctly. But now I need to find a way to ensure that the locale is UTF-8, but not necessarily English, and some might not have it installed.
smart_idiot
smart_idiot
The problem may in fact be with your system.

Make sure at least one of the environment variables LC_ALL or LANG are set to a sane locale, in this case, en_US.UTF-8. They should normally be set by your login manager, so make sure it's configured properly.

If you login from the console and use startx, you can export the variables inside ~/.xinitrc.

Failing that, you can edit ~/.bashrc which will get executed every time you open a new terminal.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.