Home Backend Development PHP Tutorial 用PHP编程读取汉字点阵数据_PHP

用PHP编程读取汉字点阵数据_PHP

Jun 01, 2016 pm 12:30 PM
o str byte data Chinese character programming read

背景知识:

  简体中文国标字库(1981年订,中国大陆)。7445个字符,其中汉字6773个,包括一级汉字3755个,二级汉字3008个。采用2字节(16位二进制)编码。

  区位码:国标GB2312规定,所有的国标汉字与符号组成一个94×94的矩阵。在此方阵中,每一行称为一个”区”,每一列称为一个”位”,因此,这个方阵实际上组成了一个有94个区(区号分别为0 1到94)、每个区内有94个位(位号分别为01到94)的汉字字符集。一个汉字所在的区号和位号简单地组合在一起就构成了该汉字的”区位码”。在汉字的区位码中,高两位为区号,低两位为位号。由此可见,区位码与汉字或符号之间是一一对应的。

  内码:汉字的内码是指在计算机中表示汉字的编码。机内码与区位码稍有区别。为什么不直接用区位码作为计算机内的编码呢? 这是因为汉字的区码和位码的范围都在1到94内, 如果直接用区位码作机内码, 就会与基本ASCII码冲突。 汉字的内码通常与所使用的计算机系统有关。目前,对于国内大多数的计算机系统,一个汉字的内码占两个字节,分别称为高位字节与低位字节,且这两位字节与区位码的关系如下: 内码高位=区码 A0H(H表示十六进制) 内码低位=位码 A0H 例如,汉字”啊”的区位码为”1601″,区码和位码分别用十六进制表示即为”1001H”,则它的内码为”B0A1H”。其中B0H为内码的高位字节,A1H为内码的低位字节。

  php代码:返回由0和1组成的字符串。

<?php
/**
* 读取汉字点阵数据
*
* @author legend <legendsky@hotmail.com>
* @link http://www.ugia.cn/?p=82
* @Copyright www.ugia.cn
*/

$str = "中华人民共和国" ;

$font_file_name = "simsun12.fon" ; // 点阵字库文件名
$font_width = 12 ; // 单字宽度
$font_height = 12 ; // 单字高度
$start_offset = 0 ; // 偏移

$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 )
 {
  // 先求区位码,然后再计算其在区位码二维表中的位置,进而得出此字符在文件中的偏移
  $offset = (( ord ( $str { $i }) - 0xa1 ) * 94 ord ( $str { $i 1 }) - 0xa1 ) * $offset_size ;
  $i ;
 }
 else
 {
  $offset = ( ord ( $str { $i }) 156 - 1 ) * $offset_size ;
 }

 // 读取其点阵数据
 fseek ( $fp , $start_offset $offset , SEEK_SET );
 $bindot = fread ( $fp , $offset_size );

 for ( $j = 0 ; $j < $offset_size ; $j )
 {
  // 将二进制点阵数据转化为字符串
  $dot_string .= sprintf ( "b" , ord ( $bindot { $j }));
 }
}

fclose ( $fp );

echo $dot_string ;
?>

  其中包括两个点阵字体文件:一个为16×16的chs16.fon,另一个为12×12的simsun12.fon,偏移均为零。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

How to install and register the btc trading app? How to install and register the btc trading app? Feb 21, 2025 pm 07:09 PM

This article will provide a detailed introduction to how to install and register a Bitcoin trading application. The Bitcoin trading app allows users to manage and trade cryptocurrencies such as Bitcoin. The article guides users through the installation and registration process step by step, including downloading applications, creating accounts, performing identity verification, and first deposit. The goal of the article is to provide beginners with clear and easy-to-understand guidelines to help them easily enter the world of Bitcoin trading.

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

Ouyi Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Mar 12, 2025 pm 05:48 PM

This article recommends the top ten digital currency trading apps in the world, including Binance, OKX, Huobi Global, Coinbase, Kraken, Gate.io, KuCoin, Bitfinex, Gemini and Bitstamp. These platforms have their own characteristics in terms of transaction pair quantity, transaction speed, security, compliance, user experience, etc. For example, Binance is known for its high transaction speed and extensive services, while Coinbase is more suitable for novices. Choosing a platform that suits you requires comprehensive consideration of your own needs and risk tolerance. Learn about the world's mainstream digital currency trading platforms to help you conduct digital asset trading safely and efficiently.

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles