Use PHP programming to read Chinese character Lattice Data _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use PHP programming to read Chinese character lattice data. Background: Simplified Chinese national standard font (set in 1981, mainland China ). 7445 characters, including 6773 first-level Chinese characters and 3755 second-level Chinese characters. Use 2 bytes (16-bit background knowledge:

Simplified Chinese national standard font (set in 1981, mainland China ). 7445 characters, including 6773 first-level Chinese characters and 3755 second-level Chinese characters. It is encoded in 2 bytes (16-bit binary.

Location Code: GB2312 indicates that all Chinese characters and symbols of the national standard form a 94 × 94 matrix. In this square matrix, each row is called a "area", and each column is called a "bit". therefore, this square matrix actually forms a Chinese character set with 94 areas (0 1 to 94 area numbers respectively) and 94 places (bits are 01 to 94) in each area. The area code and location code of a Chinese character are simply combined to form the "location code" of the Chinese character ". In the Chinese character location code, the upper two digits are the area code, and the lower two digits are the bit numbers. It can be seen that the location code corresponds to Chinese characters or symbols one by one.

Internal Code: the internal code of a Chinese character refers to the encoding of a Chinese character in a computer. The inner code is slightly different from the location code. Why not use a location code as the computer code? This is because the range of the Chinese character code and bit code is between 1 and 94. if you directly use the Field Code as the machine internal code, it will conflict with the basic ASCII code. The inner code of Chinese characters is usually related to the computer system used. Currently, for most computer systems in China, the internal code of a Chinese character occupies two bytes, namely, the high byte and the low byte. The relationship between these two bytes and the location code is as follows: internal Code high = area code + A0H (H indicates hexadecimal) internal code low = bit code + A0H for example, the Chinese character "ah" is "1601 ″, the partition code and bit code are expressed as "1h" in hexadecimal notation respectively, and the inner code is "B0A1H ". B0H indicates the high byte of the inner code, and A1H indicates the low byte of the inner code.

Php (as the mainstream development language) Code: returns a string consisting of 0 and 1.

<? Php (as the mainstream development language)
/**
* Reading Chinese character Lattice Data
*
* @ Author legend <legendsky@hotmail.com>
* @ Link: http://www.ugia.cn /? P = 82
* @ Copyright www.ugia.cn
*/

$ Str = "People's Republic of China ";

$ Font_file_name = "simsun12.fon"; // dot matrix font file name
$ Font_width = 12; // word width
$ Font_height = 12; // The height of a single word
$ Start_offset = 0; // Offset

$ Fp = fopen ($ font_file_name, "rb ");

$ Offset_size = $ font_width * $ font_height/8;
$ String_size = $ font_width * $ font_height;
$ Dot_string = "";

For ($ I = 0; $ I <strlen ($ str); $ I ++)
{
If (ord ($ str {$ I})> 160)
{
// First obtain the location code, and then calculate its position in the location code comment table to obtain the offset of this character in the file.
$ Offset = (ord ($ str {$ I})-0xa1) * 94 + ord ($ str {$ I + 1})-0xa1) * $ offset_size;
$ I ++;
}
Else
{
$ Offset = (ord ($ str {$ I}) + 156-1) * $ offset_size;
}

// Read its dot matrix data
Fseek ($ fp, $ start_offset + $ offset, SEEK_SET );

Simplified Chinese national standard font (set in 1981, mainland China ). 7445 characters, including 6773 first-level Chinese characters and 3755 second-level Chinese characters. 2 bytes (16 bits...

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.