Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ error: Conversion to Execution Character set

Tags:

c++

c

string

The problem is that I am trying to print some characters outside the ASCII like the German umlaut characters, the 'ß' and the like. These characters don’t fit in the normal char variable, so obviously I tried to put them in a wchar_t and initialized the string with a L".....". But every time this string contains a character from above, I get the mentioned error, but it is fine with all other characters of the ASCII. This also happens with u"....", U"......" and if I use the u8"....." it doesn’t generate the error but prints garbage.

So to the questions:

  1. How can I print this characters and other characters from outside the ASCII set?
  2. What is this error anyway, what does it mean?
  3. The only way I could print the characters with was by a call to setlocale() to set the locale to the environment default (German) and then by simply creating a normal array with this characters in it will generate no error and print correctly, the problem here is WHY!!? I totally don't get why this works now, as the size of the char type doesn’t get changed so I don't assume this call to setlocale() change the mapping of characters in the default character set (ASCII normally).
  4. If anyone can give a little more explanation on the subject character sets and how to handle them in C++ and C, then I would appreciate it very much especially with the UTF-8, 16, 32 (you don’t have to write it yourself even some other well organized and relatively easy to understand websites that tackle the subject more thoroughly are very welcomed).

Example as requested:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    wstring x = L"öäüß" ;

    wcout << x ;

    return 0 ;
}

This prints the error.

#include <iostream>
#include <string>
#include <locale>

using namespace std;

int main(void)
{

    setlocale(LC_ALL,""); // sets locale to german on my computer
    string x = "äöüß" ;

    cout << x ;

    return 0 ;
}

This works properly. But even after changing the locale if I make the first code the error is generated.

Note: I noticed in C++ (not C) that even without including any file else than iostream that all the contents of other files like string or locale or anything else from the standard library is declared and in std namespace. I ignored this for a while and still include the files for documentation but why is this happening. (I am using tdm-gcc 64bit)

like image 466
Lockon2000 Avatar asked Nov 01 '22 04:11

Lockon2000


1 Answers

I've faced this problem too when compiling multi-platform projects initially edited in Visual Studio. VS uses a different character set (CP1252, at least in my case), while GCC expects UTF-8. You can use the -finput-charset g++ flag to indicate the encoding of your source code.

like image 150
cbuchart Avatar answered Nov 12 '22 22:11

cbuchart