Reputation: 59
Before when I did std::cout << char(1);
I used to get the smiley face as an output, now I am getting a box with a question mark inside it. I'v been told I need to change the code page so I did with code below, but without any result, I also used SetConsoleCP(437)
.
int main()
{
SetConsoleOutputCP(437);
std::cout << char(1);
}
So please can tell me a way to output the control character of the ASCII like they used to be.
Note: This issue started when I switch from XP to Windows10.
Edit: In the this link https://ibb.co/jH1MS7q, this what i get when using chcp and all 256 character displayed in my console, what I want to print is the one in this link https://en.wikipedia.org/wiki/Code_page_437#/media/File:Codepage-437.png
Upvotes: 0
Views: 869
Reputation: 10028
The simplest way is a lookup table.
On Windows we need a little extra help to make sure things are set-up properly.
#ifdef _WIN32
#include <windows.h>
struct OutputCodePage
{
UINT original_cp;
OutputCodePage( UINT cp )
: original_cp{GetConsoleOutputCP()}
{ SetConsoleOutputCP( cp ); }
~OutputCodePage()
{ SetConsoleOutputCP( original_cp ); }
};
OutputCodePage output_code_page( CP_UTF8 );
#endif
Everything else works the same on both Windows and Linux (and OS X).
#include <iostream>
const char * CP437[] = // I choose to represent 0 (nul) as 32 (space)
{
" ", "☺", "☻", "♥", "♦", "♣", "♠", "•", "◘", "○", "◙", "♂", "♀", "♪", "♫", "☼",
"►", "◄", "↕", "‼", "¶", "§", "▬", "↨", "↑", "↓", "→", "←", "∟", "↔", "▲", "▼",
" ", "!", "\"","#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?",
"@", "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", "[", "\"","]", "^", "_",
"`", "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", "{", "|", "}", "~", "⌂",
"Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å",
"É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "₧", "ƒ",
"á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "¿", "⌐", "¬", "½", "¼", "¡", "«", "»",
"░", "▒", "▓", "│", "┤", "╡", "╢", "╖", "╕", "╣", "║", "╗", "╝", "╜", "╛", "┐",
"└", "┴", "┬", "├", "─", "┼", "╞", "╟", "╚", "╔", "╩", "╦", "╠", "═", "╬", "╧",
"╨", "╤", "╥", "╙", "╘", "╒", "╓", "╫", "╪", "┘", "┌", "█", "▄", "▌", "▐", "▀",
"α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "∞", "φ", "ε", "∩",
"≡", "±", "≥", "≤", "⌠", "⌡", "÷", "≈", "°", "∙", "·", "√", "ⁿ", "²", "■", "\xA0"
};
int main()
{
std::cout << "ASCII table:\n ";
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
std::cout << " " << CP437[y * 16 + x];
}
std::cout << "\n ";
}
}
Make sure you save the file as UTF-8 without a BOM, and compile with UTF-8 source files. (Modern compilers do that automatically, so you shouldn’t find it a problem.)
There are plenty of other ways to mess around with it, but this is the basic idea behind all translation tables.
Upvotes: 1
Reputation: 2130
The code works for me. According to SetConsoleOutputCP, it is better to use EnumSystemCodePages to Enumerate the code pages that are either installed on or supported by an operating system and Make sure your code page data file is fine.
Upvotes: 0