For compatibility M$ decided that Code page 437 ("OEM") is used for the console
Well that makes sense, really. In fact, that was roughly my guess.
Veltas wrote:
Also I think it changes the codes for different languages (to give support for other languages in a non-Unicode program), and for all we know it doesn't do that in the Command Prompt.
But this is, however, a big pain in the butt. Why they couldn't chose a code that lines up with the Western codes if they were going to go ahead and use the Latin characters in the console anyway? I don't know....
EDIT: Now that I think about it, maybe it does make sense. If the Command Prompt ran all the DOS programs back in older Windows, then it would need to use the correct characters in the console or else they wouldn't draw graphics correctly or display the program's characters correctly.
For the sake of xantavis, and I'm sorry if someone's already provided this, could someone give a standard C++ way to ensure the console outputs using the correct character code? I think suddenly having to import win32 into a beginner's C++ program is a little too much.
Here's a quick reference:
standard C:
To enable whatever user's default settings are: setlocale(LC_ALL, "");
To enable US English UTF-8: setlocale(LC_ALL, "en_US.utf8"); // not on Windows
standard C++:
user's defaults: std::locale::global(std::locale(""));
US English UTF-8: std::locale::global(std::locale("en_US.utf8")); // not on Windows
you can then apply this or different language locale to each stream: std::cout.imbue(std::locale()); // apply whatever was last set via global
On Windows, there are no Unicode locales, but there are standard C++ locale-independent Unicode conversions (since VS2010), and, since VS2005, there are Windows-only ways to switch the console to Unicode:
1 2 3
_setmode(_fileno(stdout), _O_U16TEXT); // use UTF-16
_setmode(_fileno(stdout), _O_U8TEXT); // use UTF-8
_setmode(_fileno(stdout), _O_WTEXT); // use wstrings
(works on files even better, since the console fonts may be lacking)
It's a shame that our Windows C++ compilers don't seem to be able to cope with stuff like making sure our strings work in the Command Prompt, but then again it's the Command Prompt's fault as streams are supposed to be general so everything else would stop working if we changed it for Command Prompt.
Unfortunately it shows only a single code page [after fix: it shows all kinds of code pages] for both (while it has no problem to SetConsoleOutputCP(CP_UTF8) , the output is still wrong) on my system (Windows 7)
Thanks, but actually, my goal was to create a string with as little lines as possible, that uses non/english characters. Well I just found an easy way, just by myself! I do following:
1 2 3 4 5 6
#include <iostream>
usingnamespace std;
int main () {
char mepris [6] = {'m', 130, 'p', 'r', 'i', 's'};
and if I want to print it out, I just create a for loop!
#include <iostream>
usingnamespace std;
int main () {
char mepris [] = {'m', 130, 'p', 'r', 'i', 's', 0};
you don't need a loop
I mean you can do so, but if you have strings of unknown content you have to map the characters. It'd be no more than a function call when you want to display the string.
It's not that unusual that you have a certain intern format and for different occasions (i/o such as GUI/file etc) you need convert it ot another format