C++ Chess Board Printing in Windows Using CodePage 437 – Extended ASCII


Well, most people, when they start programming in C/C++, they would like to print something cool under console.  Console, without graphics, is suitable for plain-text, i.e. ASCII code from 1-127. The extended ASCII code, which is from 128 to 255, may not be printable/visible unless you set to a right code-page. The code page sometimes is referred to character encoding. It consists of a table of values that describes the character set for a particular language.

The Original IBM PC hardware code page is 437, which gives the following characters.

437 C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII c / c++ code code library implementation programming languages tricks Win32 API windows command shell

There are nice lines and block patterns included in codepage 437. The 7-bit ASCII (up to 127)  can be almost printed/shown under every console without problem. However, if you compile and run the following C++ program at Windows/Linux, you will just get a mess of crap characters printing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
 
using namespace std;
 
const char BLACK = 219;
const char WHITE = 32;
const int SIZE = 8;
const int BLOCK = 3;
 
int main()
{
    for (int i = 0; i < SIZE; i ++) {
        for (int u = 0; u < BLOCK; u ++) {
            for (int j = 0; j < SIZE; j ++) {
                int c = (i + j) & 1;
                for (int k = 0; k < BLOCK; k ++) {
                    if (c == 1) {
                        cout << BLACK;
                    } else {
                        cout << WHITE;
                    }
                }
            }
            cout << endl;
        }
    }
    return 0;
}
#include <iostream>

using namespace std;

const char BLACK = 219;
const char WHITE = 32;
const int SIZE = 8;
const int BLOCK = 3;

int main()
{
    for (int i = 0; i < SIZE; i ++) {
        for (int u = 0; u < BLOCK; u ++) {
            for (int j = 0; j < SIZE; j ++) {
                int c = (i + j) & 1;
                for (int k = 0; k < BLOCK; k ++) {
                    if (c == 1) {
                        cout << BLACK;
                    } else {
                        cout << WHITE;
                    }
                }
            }
            cout << endl;
        }
    }
    return 0;
}

We want to print a nice chess board (8 X 8) using ASCII 219 (black square). However, it gives us the following:

extended-ascii C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII c / c++ code code library implementation programming languages tricks Win32 API windows command shell

So, what happens? The extended ASCII is not visible under ‘incorrect’ codepage. So, under windows command shell, we can execute the command chcp 437 to change to code page 437.

codepage437 C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII c / c++ code code library implementation programming languages tricks Win32 API windows command shell

Now, the console font style changes, we can run the program again, and this time we have a nice chess board.

chess C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII c / c++ code code library implementation programming languages tricks Win32 API windows command shell

This is probably the simplest thing without using other methods, such as unicode methods (wcout, wchar)

Alternatively, on windows platforms, you can use SetConsoleOutputCP to force the code page in the output. The SetConsoleOutputCP is the Windows 32 API, so it can be found under windows.h.

From: Here.

3f6485a33bdeb067681cea9641e0077b.jpg C++ Chess Board Printing in Windows Using CodePage 437 - Extended ASCII c / c++ code code library implementation programming languages tricks Win32 API windows command shell

The source is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* https://HelloACM.com */
#include <iostream>
#include <windows.h>
 
using namespace std;
 
const unsigned char INT1 = 244;
const unsigned char INT2 = 245;
 
int main()
{
    SetConsoleOutputCP(437);
    cout << INT1 << 'b' << endl;
    cout << '|' <<  "   f(x) dx = F(b) - F(a) " << endl;
    cout << INT2 << 'a' << endl;
    return 0;
}
/* https://HelloACM.com */
#include <iostream>
#include <windows.h>

using namespace std;

const unsigned char INT1 = 244;
const unsigned char INT2 = 245;

int main()
{
    SetConsoleOutputCP(437);
    cout << INT1 << 'b' << endl;
    cout << '|' <<  "   f(x) dx = F(b) - F(a) " << endl;
    cout << INT2 << 'a' << endl;
    return 0;
}

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
584 words
Last Post: Say Hello in VBScript/Javascript COM Automation (WSC)
Next Post: How to Implement a LRU (Least Recently Used) Cache in C++?

The Permanent URL is: C++ Chess Board Printing in Windows Using CodePage 437 – Extended ASCII

Leave a Reply