1

I'm new in c++, and I tried to write a very simple code, but the result is wrong, and I don't know how to fix it.

The code is:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string test_string = "aáeéöôőüűč♥♦♣♠";
    cout << test_string << endl;
    return 0;
}

But the result is: a├íe├ę├Â├┤┼Ĺ├╝┼▒─ŹÔÖąÔÖŽÔÖúÔÖá

I am on Windows, using Code::Blocks.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
eqiproo
  • 1,736
  • 4
  • 17
  • 18
  • 1
    The result isn't as much "wrong" as it is "unexpected". You need to broaden your expectations and understanding of your environment. – Kerrek SB Sep 19 '12 at 12:57
  • Works for me. What operating system are you using? Compiler? Source file encoding? –  Sep 19 '12 at 12:57
  • 1
    good luck trying to display utf-8 in a windows console, see: http://stackoverflow.com/questions/379240/is-there-a-windows-command-shell-that-will-display-unicode-characters you will have to change the codepage prior to running your program. – SirDarius Sep 19 '12 at 13:15
  • 1
    What I always do -- which might or might not be the answer you are looking for -- is to encode the string as UTF-8 in a tool and put it in as escape codes, byte for byte, like `test_string = "a\xc3\xa1""e\xc3\xa9\xc3\xb6\xc3\xb4\xc5\x91\xc3\xbc\xc5\xb1\xc4\x8d\xe2\x99\xa5\xe2\x99\xa6\xe2\x99\xa3\xe2\x99\xa0"`. This way I am sure that the string is correct UTF-8 no matter the encoding of the source code file or any locale settings. – Magnus Hoff Sep 19 '12 at 13:15
  • Are you limited to standard C++, or are you willing to use Windows APIs? – Magnus Hoff Sep 19 '12 at 13:31

2 Answers2

1

Save file as UTF-8 without BOM signature, and try use printf().

//Save As UTF8 without BOM signature
#include <stdio.h>
#include <windows.h>

int main() {
    SetConsoleOutputCP(65001);
    char test_string[] = "aáeéöôőüűč♥♦♣♠";
    printf(test_string);
    return 0;
}

And the result is: aáeéöôőüűč♥♦♣♠

vladasimovic
  • 310
  • 3
  • 5
0

Unfortunately working with UTF-8 on Windows is very problematic.

On Linux, you can simply wstring like this:

Does this code work universaly, or is it just my system?

But unfortunately Windows doesn't have an UTF-8 locale, so you are left with Windows API.

http://www.siao2.com/2007/01/03/1392379.aspx

Community
  • 1
  • 1
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151