您的位置:首页 > 编程语言 > C语言/C++

输出扩展ASCII码-如何输出小白块

2017-04-07 19:20 288 查看

Output the Extended ASCII(E-ASCII) in C - How to print the little white block in CMD window

      When write the EX1-13 in The C Programming Language, I need to  print the littile white block in CMD window.. Since the white block is included by the E-ASCII, my task is to find how output the E-ASCII.

     The E-ASCII of  white block is 219, therefore I shall ouput it with the following codes:

#include<stdio.h>
void main()
{
printf("%c\n",219);
}

   However, the screen prints the "?". Obviously, there is something wrong.

   By searching on the internet, I find two solutions:

1. Output double byte characters

#include<stdio.h>
void main()
{
printf("%c%c\n", 0xa8, 0x80);
}

   //输出双字节字符来实现输出小方块。这种方法的原理不太清楚,需要进一步了解。此外,方块占据普通字符两倍的宽度,使画图布局变得困难。

   With these codes, I get the desired result. But, I still don't know why it happens. I will take some time to find out the principle of printing double byte character.

   Besides, the printed block will occupy double spaces as general character dose, which makes the layout difficult.

2. Output E-ASCII characters

   The code page of the CMD window is 936(ANSI/OEM-GBK)by default to display Simplified Chinese. We have to change the code page to 437(OEM-American) to display all of the Extended ASCII.

#include<stdio.h>
#include<windows.h> // This header must be included.
void main()
{
SetConsoleOutputCP(437);
printf("%c\n",219);
}

   //更改代码页变量来实现直接输出扩展ASCII码,方便、易懂。方块与普通字符占据相同空间,便于布局。

    Then the problem is perfectly fixed. The printed block occupy the same place as general character dose.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息