简体   繁体   中英

How to output low ASCII using C++ in Windows 10?

I'm trying to output directional arrows for a simple snake game in C++ on Windows 10. However, using this table as reference:

ASCII reference

All I got is this tiny question mark in the console:

Tiny question mark

I would like to output the symbols 16, 17, 30 and 31. I'm not much of programmer so it could be some basic mistake, but some symbols do work while others result in that symbol above.

A small example:

void showSnake() {
    char snakeHead;
    snakeHead = 31;
    cout << snakeHead; //THIS SHOWS THE TINY QUESTION MARK
    snakeHead = 62;
    cout << snakeHead; //THIS SHOWS THE ">" SYMBOL
}

You should use Unicode, you'll have much more choices for characters.

On https://en.wikipedia.org/wiki/List_of_Unicode_characters I found this symbol '▶' which looks similar to what you wanted to use.

Its unicode value is U+25BA which means you can create a character with a value of '\►' in C++.

In practice however that value would go outside the range of the char type so you have to use wide characters ( wchar ) to get the job done.

As per this answer you should also toggle support for Unicode character in stdout using the _setmode function (see here ) from the C run-time library.

#include <iostream>
#include <io.h>

int main() {
     _setmode(_fileno(stdout), _O_U16TEXT);
     std::wcout << L'\u25BA';
}

The setlocale() line is in case the default locale doesn't support non-ACSII output (it didn't on my machine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM