How Exactly Do I Deal With Japanese Characters in C++?

Andy Ibanez picture Andy Ibanez · Dec 17, 2011 · Viewed 8.1k times · Source

I'm trying to do something as simple as this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    wstring nihongo = L"みんなのにほんご";
    wcout << nihongo << endl;
    return 0;
}

But I get the following errors:

C:\Users\Leonne\Leomedia\MetaDatterTest.cpp|7|error: stray '\201' in program|

C:\Users\Leonne\Leomedia\MetaDatterTest.cpp|7|error: stray '@' in program|

C:\Users\Leonne\Leomedia\MetaDatterTest.cpp||In function 'int main()':|

C:\Users\Leonne\Leomedia\MetaDatterTest.cpp|7|error: converting to execution character set: Illegal byte sequence|

||=== Build finished: 3 errors, 0 warnings ===|

I'm in a Windows machine and I am attempting to make a library that is as portable as possible, and it must be able to deal with any kind of characters: Russian, Japanese, ASCII, everything.

Answer

Shane Powell picture Shane Powell · Dec 17, 2011

Visual Studio support unicode source files. Make sure that your cpp files are saved a utf16 or utf8 formatted files with a BOM. Once in that format your files will compile fine.