简体   繁体   English

是否可以更改 C 中的字符集编码?

[英]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.我想让我的 C 程序从其他字符集打印符号,而不是从 ASCII 表,因为它是默认的。 For example, I want to print chars in range [200,250] from the ISO-8859 charset.例如,我想从 ISO-8859 字符集中打印 [200,250] 范围内的字符。 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: Output:

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

In fact C functions don't care about encoding, so if you have a code like that:事实上 C 函数并不关心编码,所以如果你有这样的代码:

#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.它将完全按照 C 源文件的编码打印出“德国”“Hello World”,与系统设置等无关。当然,如果您打印从文件中读取的字符串,也是如此。 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如果您想重新编码字符串(例如从 UTF-8 到 ISO-8859),您必须手动执行或搜索适当的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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