简体   繁体   中英

Is it possible to change charset encoding in C?

I would like to make my C program print signs from other charset, not from the ASCII table as it is default. For example, I want to print chars in range [200,250] from the ISO-8859 charset. Is it possible at all? How the compiler should be set? Thanks in advance for help!

Setting you locales and using wide characters:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void) 
{
    setlocale(LC_CTYPE, "");

    for (wchar_t c = 200; c < 250; c++)
    {
        wprintf(L"%lc", c);
    }
    wprintf(L"\n");
    return 0;
}

Output:

ÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øù

In fact C functions don't care about encoding, so if you have a code like that:

#include <stdio.h>

int main( void )
{
    printf( "Hällo Wörld\n" );
    return( 0 );
}

It will print out 'germanzied' "Hello World" in exactly the encoding of the C source file, indepedent from system settings etc. The same is true of course if you print strings that you read from a file. If you want to reencode strings (say from UTF-8 to ISO-8859) you have to do t manually or search for an appropriate library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM