Klas. S Klas. S - 2 years ago 86
C Question

Print braille characters in ncurses

I attempted to print braille characters in ncurses.

This is my code:

#include <ncurses.h>

char *str =
" ⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏\n"
"⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟\n"
"⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯\n"
"⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿\n";


int main(int argc, const char *argv[]) {
initscr();
printw("%s", str);
getch();
printf("%s", curses_version());
endwin();
printf("%s", str);
return 0;
}


The output is:

?~A?~B?~C?~D?~E?~F?~G?~H?~I?~J?~K?~L?~M?~N?~O
?~P?~Q?~R?~S?~T?~U?~V?~W?~X?~Y?~Z?~[?~\?~]?~^?~_
⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯
⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿


How do I output all characters correctly?

Update: I also tried
printf
which seems to work,
addstr
produces the same output as
printw
.

If I change the locale with
setlocale(LC_ALL, "");
I get the output:

A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯
⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿

Answer Source

With some experimentation, I found what is probably wrong:

  1. Your program uses the C locale by default. This assumes ASCII encoding. It's not a problem when you output multi-byte characters with stdio, because these functions just deliver the bytes as is to the console. But ncurses actually uses the locale, so it can know things like how many bytes make a character (important for exact positioning) etc. Change your program as following:

    #include <curses.h>
    #include <locale.h>
    
    int main(int argc, char *argv[])
    {
        // initialize locale to system's default:
        setlocale(LC_ALL, "");
    
        // now init and use curses ...
    
  2. If you still get garbled output, your system's libncurses doesn't handle unicode. In that case, link ncursesw instead of ncurses and you should be done.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download