Home > Software design >  How would I print a multi-line (non-standard) unicode string of text in C ?
How would I print a multi-line (non-standard) unicode string of text in C ?

Time:09-11

I'm currently new to C , but I've written a similar bit of string code in Python before (unrelated, I know). I'm struggling to find out how to print non-standard unicode characters through cout. Below is a copy of my code alongside a copy of the unicode i've been trying to print.

    #include <iostream>

using namespace std;

int main() {


   string name;

   name = "▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n"
          "███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗\n"
          "████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║\n"
          "██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║\n"
          "██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║\n"
          "██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║\n"
          "╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝\n"
          "▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀";
   cout << name << endl;


    return 0;
}

The code's output is as follows:

ΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇ ΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇΓûÇ

Process finished with exit code 0

That's not the full output, but the text simply repeats to fill the unicode character's spaces.

How would I go about printing something like this (preferably without having to single-print unicode characters to match the setup)?

Many Thanks!

CodePudding user response:

AFAIK there is no standard way in C to process unicode in standard io, and different OSs' default consoles will behave differently if you just make the unicode string as std::string to output; E.g. in Windows cmd maybe you need _setmode(_fileno(stdout), _O_U16TEXT); to show UTF-16 strings correctly.

Good news is that in C 23 there will be <print> to (hopefully, not definitely so far) help solve this troublesome problem.

CodePudding user response:

There is no problem with your program. There is a problem with your display.

% cc -o modeni modeni.cpp -lc  
% ./modeni
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
███╗░░░███╗░█████╗░██████╗░███████╗███╗░░██╗██╗
████╗░████║██╔══██╗██╔══██╗██╔════╝████╗░██║██║
██╔████╔██║██║░░██║██║░░██║█████╗░░██╔██╗██║██║
██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║╚████║██║
██║░╚═╝░██║╚█████╔╝██████╔╝███████╗██║░╚███║██║
╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚═╝░░╚══╝╚═╝
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
% echo $TERM
xterm-256color

I am using a macOS system and this works with TERM=xterm-256color on both iTerm2 and Terminal.

What is TERM?

This environment variable advises applications what terminal emulation is required to display characters on screen properly. An application will use the termcap/terminfo database to look up the proper escape sequences to display colors, move or manipulative text on screen, and other effects.

I only ever see xterm-256color now. Why?

Modern terminal applications assume the output will be in xterm-256color format. Many no longer have an option to choose another format.

  • Related