简体   繁体   English

如何通过C程序打印127到160的扩展ASCII字符?

[英]How to print Extended ASCII characters 127 to 160 in through a C program?

I am trying below code to print all the ASCII characters, but it does not print anything for 127 to 160. I know they are Control Chracters set or some Latin/ Spanish characters. 我正在尝试使用下面的代码来打印所有ASCII字符,但对于127至160则不打印任何内容。我知道它们是控制字符集或某些拉丁文/西班牙文字符。 If the same characters are copy pasted from Windows, it prints well in unix. 如果从Windows复制粘贴相同的字符,则它在unix中打印得很好。 Why not throug a C program? 为什么不通过C程序呢?

#include <stdio.h>

int main()
{
    int i;
    char ch;

    for(i = 0; i < 256; i++)
    {
        printf("\n%03d %02x %02c",i ,i ,i);
    }
}

ASCII is a 7-bit code. ASCII是7位代码。 The interpretation of byte values above 128 is dependent upon the OS, your locale/language settings, and so on. 字节值大于128的解释取决于操作系统,语言环境/语言设置等。 They are not standard. 它们不是标准的。 Under Windows in English, they are most commonly defined by CP1252; 在英文版的Windows下,它们最常由CP1252定义; on Linux they are more commonly ISO-8859-1. 在Linux上,它们通常是ISO-8859-1。 Some OSs use UTF-8, which is not a character set itself but a way to encode Unicode into an 8-bit stream by using more than one byte for most characters. 某些操作系统使用UTF-8,它本身不是字符集,而是通过对大多数字符使用多个字节来将Unicode编码为8位流的方法。 If you really need to work with characters outside the standard ASCII 32-126, you should really be using wide characters and locale stuff. 如果你真的需要使用标准ASCII 32-126之外的字符,你应该使用宽字符和区域设置。

BTW, character 127 is a special case: it's the control character "rubout", which denotes erased data. BTW,字符127是一种特殊情况:它是控制字符“rubout”,表示擦除数据。 (This was done so that a section of paper tape could be erased by punching all the holes!--yes, some of us are old enough to remember paper tape). (这样做的目的是可以通过打孔来擦除一部分胶带!-是的,我们中有些人的年龄足以记住胶带)。

You might want to look into setlocale. 您可能想要查看setlocale。 I don't know which set of characters you're looking for but you could try setlocale (LC_ALL,""); 我不知道你在寻找哪一组字符,但你可以尝试setlocale (LC_ALL,""); to set your printed characters to match the environment (which seems to match your requirement since copy-pasting worked). 设置您要打印的字符以匹配环境(由于粘贴粘贴有效,这似乎符合您的要求)。

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

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