Coding in C ++

2

Hi, I would like to know how to change the output encoding of C ++, knowing that the default encoding is ASCII.

I have already tried with the <windows.h> and <tchar.h> libraries, in the following code:

int main(){
    _tsetlocale(LC_ALL, _T("portuguese"));
    (...)
}

But I realize that besides not working in other OS, just go to Portuguese.

I want to use several characters of several languages in the application but I have no idea how to do it.

Is UTF-8 an option? If so, how do I implement it?

    
asked by anonymous 02.08.2014 / 18:40

1 answer

2

Stay away from the "set locale" functions (such as tsetlocale). They do not have to do with your problem right now.

And what our colleague hugomg said is right. The output of your program are bytes (type, are numbers). And as they will be shown on the screen of ANY operating system, it depends on the program execution environment. To give an example, imagine a simple program that just writes "Hi, world!" and ends. If we run this program on a terminal with different coding than the BYTES of that string in the program have, the phrase will appear in a crazy way. If it's right, it works. That's all.

So, do a little research on the following (Wikipedia is a teacher for this). 1) ASCII encoding. 2) ISO-8859-1 coding. 3) UTF-8 encoding. Remembering that UTF-8 no longer matches 1 byte per symbol, as in the others I mentioned.

My C programs use accents in printfs strings. And this works fine as long as the strings in the source code have the same encoding as the terminal / screen where the program will run. Usually choosing the common coding of the target system already guarantees this. ISO-8859-1 and UTF-8 have been the best choices in 90% of cases. And the cases apart are easy to change, when you need to. Do you understand this paragraph?

Finally, there is a function and / or a library and / or a program called iconv. With this you can convert your strings from one encoding to another desired one. You can help too.

In short:

  • find and choose the encoding that all strings in your source code have; it depends on your editor, it's simple.

  • Choose the encoding your program will have on exit, and this will become a requirement for anyone using it; the person's thermal / screen will naturally understand the strings of your program. UTF-8 or ISO-8859-1 are great bets, in my opinion.

  • Finally, you can use the iconv () function when something more "detailed" is needed.

  • Useful readings:

    link

    link (in Portuguese, pro basic only, article is not as complete yet)

    link (iconv function manual)

    link

    link

        
    03.08.2014 / 06:59