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

▼魔方 西西 提交于 2019-12-03 22:19:40

问题


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. If the same characters are copy pasted from Windows, it prints well in unix. Why not throug a C program?

#include <stdio.h>

int main()
{
    int i;
    char ch;

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

回答1:


ASCII is a 7-bit code. The interpretation of byte values above 128 is dependent upon the OS, your locale/language settings, and so on. They are not standard. Under Windows in English, they are most commonly defined by CP1252; on Linux they are more commonly 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. If you really need to work with characters outside the standard ASCII 32-126, you should really be using wide characters and locale stuff.

BTW, character 127 is a special case: it's the control character "rubout", which denotes erased data. (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).




回答2:


You might want to look into setlocale. I don't know which set of characters you're looking for but you could try setlocale (LC_ALL,""); to set your printed characters to match the environment (which seems to match your requirement since copy-pasting worked).



来源:https://stackoverflow.com/questions/16359225/how-to-print-extended-ascii-characters-127-to-160-in-through-a-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!