【转】打印Ascii码
ASCII码被人熟知,它的全称为American Standard Code for Information Interchange,即美国信息交换标准码。标准ASCII码的范围是0x00 - 0x7F,一共128个字符;扩展ASCII码范围是0x80 - 0xFF,一共也是128个字符。先编写程序将ASCII码表打印到Console窗口中,代码如下所示:
- #include<stdio.h>
- #include<stdlib.h>
- #include<Windows.h>
- void drawLine(char t)
- {
- for(int i = 0; i < 80; ++i)
- printf_s("%c",t);
- putchar('\n');
- }
- int main()
- {
- SetConsoleOutputCP(437);
- for(int i=0;i<32;++i)
- {
- if(i == 0)
- {
- const char *title = "Std Ascii";
- printf_s("%44s\n",title);
- drawLine('-');
- }
- if(i == 16)
- {
- const char *title = "Ext Ascii";
- printf_s("\n%44s\n",title);
- drawLine('-');
- }
- int k = i;
- if(i >= 16)
- {
- k += 112;
- }
- for(int j=0;j<8;++j)
- {
- if(j == 0)
- {
- if(k >= 7 && k <= 10 || k == 13)
- {
- printf_s("%3d%3c",k,32);
- }
- else
- {
- printf_s("%3d%3c",k,k);
- }
- }
- else
- {
- printf_s("%7d%3c",k+16*j,k+16*j);
- }
- }
- printf_s("\n");
- }
- printf_s("\n");
- system("pause");
- return 0;
- }
SetConsoleOutputCP(437)是Windows API函数,设置Console的code page(即字符集)为OEM - US,这样才能正确显示扩展ASCII码。码值7、8、9、10以及13是转义字符,会导致打印出的结果无法对齐,此例中我将它们简单的用空格(space,码值是32),程序运行的结果如下图所示: