Search code examples
c++macosencodingspecial-charactersncurses

How to print special characters in c++ for mac using ncurses libarary


Now, I want to print special characters in c++ for mac(mojava, 10.14.4), but these characters are broken in my mac book.

I installed ncurses such as brew install ncurses and brew link ncurses. It can printw English such as Hello World, but special characters such as ████████╗███████╗████████╗██████╗ ██╗███████╗ is broken.

source code

setup

initscr();
clear();
noecho();
cbreak();
curs_set(0);

work

int startX = 0;
int startY = 0;

mvprintw(startY++, startX, "Hello World");
mvprintw(startY, startX, "████████╗███████╗████████╗██████╗ ██╗███████╗\n");

Result

Hello World
?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~U~W?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H
?~U~W?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~U~W?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H
?~U~W ?~V~H?~V~H?~U~W?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~V~H?~U~W

Solution

  • This seems to work for c++ with clang++ for mac(Mojave, 10.14.4). I think it is using the mac curses rather than the brew installed ncurses. It looks like it needs the locale set: setlocale(LC_ALL, ""); For me, this cleared the screen and displayed:

    Hello World ████████╗███████╗████████╗██████╗ ██╗███████╗

    Compile cmd:
    clang++ -Wall -Wextra -Weverything -lncurses *.cpp -o prg

    main.cpp code:

    #include <curses.h>
    #include <locale.h>
    
    int main()
    {
        setlocale(LC_ALL, "");
        initscr();                /* Start curses mode */
        clear();
        noecho();
        cbreak();
        curs_set(0);
    
        int startX = 0;
        int startY = 0;
        mvprintw(startY++, startX, "Hello World");
        mvprintw(startY, startX, "████████╗███████╗████████╗██████╗ ██╗███████╗\n");
    
        refresh();                /* Print it on the real screen */
        getch();                  /* Wait for user input */
        endwin();                 /* End curses mode */
        return 0;
    }