简体   繁体   English

在C,Linux中打印扩展的ASCII字符

[英]Print extended ASCII char in C, Linux

Im trying to print all extended ASCII chars. 我正在尝试打印所有扩展的ASCII字符。 I have found a code on forum: 我在论坛上找到了一个代码:

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

int main(void) {
    wchar_t c;
    setlocale(LC_ALL, "C");

    for (c = 128; c < 256; c++) {
        wprintf(L"char nr %d: %lc\n", c, c);
    }
    printf("\n%s\n", setlocale(0, NULL));

    std::cin.get();
    std::cin.get();
return 0;
}

This code is working in windows in VS 2017. 该代码在VS 2017中的Windows中运行。 在此处输入图片说明

In the screenshot, you can see the result of this code in windows and Linux. 在屏幕截图中,您可以在Windows和Linux中查看此代码的结果。 I know that problem is with coding, but I don't know how to fix it. 我知道问题出在编码,但我不知道如何解决。

Ok, all is fine. 好的,一切都很好。 ASCII characters are limited below 128. What come above depends on the actual character set. ASCII字符限制在128以下。以上内容取决于实际字符集。

In Linux, you are displaying a subset of the ISO-8859-1 (aka Latin1) character set, while on Windows you are displaying the Windows code page 850. As you declare a UTF8 charset on Linux, you should only display error characters, but your terminal seems to interpret some bytes as latin1. 在Linux中,您显示的是ISO-8859-1(又名Latin1)字符集的子集,而在Windows上,则显示Windows代码页850。在Linux上声明UTF8字符集时,您应仅显示错误字符,但是您的终端似乎将某些字节解释为latin1。

If you want to display all Latin1 characters, just change the LANG environment variable: 如果要显示所有Latin1字符,只需更改LANG环境变量:

export LANG=pl_PL.ISO-8859-1

Or as you language seems to be Polish, ISO-8859-2 is probably more appropriate: 或者您的语言似乎是波兰语,ISO-8859-2可能更合适:

export LANG=pl_PL.ISO-8859-2

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

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