For how to use C to display Chinese Characters 3: display Chinese and English characters in graphic mode

Source: Internet
Author: User
Tags print format
With the previous theories, it is not that difficult for us to use C to display Chinese characters.
Now, one problem is that when a string contains Chinese and English characters, one English character occupies only one byte because one Chinese Character occupies two bytes.

And their internal code is different. If we still use the method described above, we will certainly print a bunch of garbled characters.

In theory 1, all strings are Chinese characters to avoid this problem.

So how can we solve the problem of mixed display in Chinese and English? In fact, it is not difficult to carefully read the first few paragraphs of theory 1 and you will find that "... as a result, our dos predecessors thought of a way to represent Chinese characters in a group of two numbers, namely, the inner code of Chinese characters, which are rarely used in the ASCII table. The remaining 128 characters are reserved for English characters, that is, the English internal code... ". These words imply a method to distinguish Chinese characters from English characters, that is:
Reads a byte of data from a string. When the maximum binary value is 1, the current character and the next character constitute a Chinese character. The next time you read data from the lower byte
Reads a byte of data from a string. If the maximum binary value is 0, the current character is an English character. The next time you read data from the next byte of the byte

If the theory is hard to understand, let the facts explain everything.

/*
* Display in Chinese and English in graphic mode
* Lo several times and VE
* Download the library
* Place the library file in the compiling directory [or change path parameters]
* Hzk16 file:
* Download a font file
*/
# Include
# Include

# Define Maxx 640/* screen width */
# Define Maxy 480/* screen height */
# Define width 20/* width of each Chinese character */
# Define high 20/* Height of each Chinese character */
# Define chsiz 2/* English font size */

Int prichi (unsigned char *);/* Chinese print function, input parameter: Chinese array pointer return value-1 exception 0 normal */

Int x = 0;/* The global variable x y controls the Chinese print format */
Int y = 0;

Int main ()
{
Char Chinese [] [60] = {/* TC compiler does not support long lines. Therefore, Chinese strings are saved in two-dimensional form */
"I gave up Tsinghua computing ?????! @! The news of selecting to take the college entrance examination for professional A is shocking ",
"I was surprised to hear from the principal, the class teacher, and the teachers in various subjects ",
"This is a program for Type whinese words ",
"And English words. djfkjaiejfjas> ifojaewkfjaweifjaokjdsfjiaew ",
"Speaking of this special award, it is really interesting. Although it is called the National Finals, it is nothing more than a few ",
"A computer program will be compiled for a total of three hours. The required language is the western language or ",
"It took me half an hour to complete the program in two languages ",
"It is a waste of my great youth, so I decided to write it again in assembly language ",
"I'm going to write it in a micro-operating hexadecimal code, but I have to give up on time ",
"It's the gold that will always shine. This is the only time I am devoted to programming ",
"But I don't know that I have been noticed by someone for a long time. After three hours, I walked out of the test room ",
"I was excited to say that the voice of the child directly led me to years ",
"Later, I often woke up from a nightmare. At the same time, I refused the invitation from Tsinghua to directly guide us ",
"Back to the scene of the incident, I jumped my head and turned it back to a thin old man ",
"It seems that my eyes are shining with my hands ",
"I suddenly rushed forward to drill out, and I was so clever that I quickly shook my hands and the old man seemed to feel ",
"The other way is to tell me that I'm from Tsinghua University ",
"The head of the computing system was invited to watch the competition. By the way, see if there are any people that can be mined ",
"That's why you understand what I mean. This sentence affects the understanding of Tsinghua students in my mind ",
"Do you mean that I am a traceable person? I nodded my head and looked at me ",
"I was very excited to understand and then began to explain to me the practical experience of Tsinghua computer ",
"How to be strong, how to be influential at home and abroad ",
"When I read my eyes, I told you seriously that he would welcome me to Tsinghua computer science ",
"I am so patient with his swordsmanship that I have to go back ",
"Discuss with your parents and escape. Of course, the first place to go is the bathroom ",
"My face, who knows whether the spof of the old man will bring my face to the school ",
"After worship, I received a formal invitation from Tsinghua, and the news was quickly completed by my class teacher's mouth ",
"School spread and then quickly summarized to my parents through various channels. "};

If (-1 = prichi (Chinese ))
{/* Print exception */
Printf ("press any key to exit ...");
Fflush (stdin );
Getch ();
Return 1;
}
Return 0;
}

Int prichi (unsigned char * Chi)
{
Unsigned char charc [2];
Unsigned char mat [16] [2];
Int I = VGA, j = vgahi, K;
Int sec, pot;
File * hzk;
If (hzk = fopen ("hzk16", "rb") = NULL)/* Open the library file */
{/* Failed to open the library */
Printf ("Open Style File (hzk16) failed! \ N ");
Return-1;
}
Initgraph (& I, & J, "");/* graph Mode Initialization */
Settextstyle (0, 0, chsiz);/* initialize English characters */
While (* Chi)
{/* Print a Chinese or English character in graphic mode every cycle */
If (* Chi & 0x80)/* the highest bit is 1 -- Chinese character */
{
SEC = * chi-0xa0;/* Get the area code for Chinese characters */
Pot = * (Chi + 1)-0xa0;/* obtain the bit code of a Chinese character */
Fseek (hzk, (94 * (sec-1) + (pot-1) * 32l, seek_set );
Fread (MAT, 32, 1, hzk );
For (j = 0; J for (I = 0; I for (k = 0; K if (MAT [J] [I] & (0x80> K )) /* The bit whose test value is 1 is displayed */
Putpixel (x + I * 8 + k, Y + J, white );
Chi + = 2;/* The Pointer Points to the next Chinese character */
}
Else/* the highest bit is 0-English character */
{
Charc [0] = * Chi;/* prepare for the output of English characters */
Charc [1] = '\ 0 ';
Outtextxy (X, Y, charc);/* Send a string at a specified position (in graphic mode */
Chi ++;/* The Pointer Points to the next English character */
}

X + = width;/* the abscissa of the next character */
If (x> = Maxx)/* full line */
{
X = 0;/* return to zero */
Y + = high;/* Y coordinate of the next character */
If (Y> = Maxy)/* Full page */
{
Printf ("press any key to print other words ");
Getch ();
System ("CLS");/* clear screen */
Y = 0;/* return to zero */
}
}
}
Getch ();
Closegraph ();/* shut down the graphics system */
Fclose (hzk);/* close the font file */
Return 0;
}

Through this post, you should have a deeper understanding of character display.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.