PHP reads Chinese character Lattice Data

Source: Internet
Author: User
Background: Simplified Chinese national standard font (set in 1981, mainland China ). 7445 characters, including 6773 Chinese characters, including 3755 first-level Chinese characters and 3008 second-level Chinese characters. It is encoded in 2 bytes (16-bit binary. Location Code: GB2312, all

Background:

Simplified Chinese national standard font (set in 1981, mainland China ). 7445 characters, including 6773 Chinese characters, including 3755 first-level Chinese characters and 3008 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 (ranging from 0 1 to 94) and 94 places (ranging from 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 codes correspond to Chinese characters or symbols one by one.

Internal Code: the internal code of Chinese characters refers to the encoding of Chinese characters in the computer. The inner code is slightly different from the location code. Why not simply use a location code as the computer's encoding? This is because the range code and bit Code of Chinese characters are both within 1 to 94. if the Field Code is directly used as the internal code, it will conflict with the basic ASCII code. The internal 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, which are distinguished by the high and low bytes. The relationship between the two bytes and the location code is as follows: internal Code high = area code A0H (H represents hexadecimal) internal code low = location code A0H for example, the Chinese character "ah" is "1601 ″, the difference between a code and a bit code is expressed as "1h" in hexadecimal notation, and its inner code is "B0A1H ". B0H indicates the high byte of the inner code, and A1H indicates the low byte of the inner code.

PHP code: returns a string consisting of 0 and 1.

<? Php
/**
* Reading Chinese character Lattice Data
*
* @ Author legend
* @ Link: http://www.ugia.cn /? P = 82
* @ Copyright www.ugia.cn
*/

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

$ Font_file_name = 'simsun12. Fon'; // the file name of the dot matrix font library.
$ 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, and then 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 );
$ Bindot = fread ($ fp, $ offset_size );

For ($ j = 0; $ j <$ offset_size; $ j)
{
// Convert binary dot matrix data into strings
$ Dot_string. = sprintf ('B', ord ($ bindot {$ j }));
}
}

Fclose ($ fp );

Echo $ dot_string;
?>

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.