关于字符编码的问题?
中文在gb2312编码的时候是2字节,但在unicode编码的 情况下是1-3字节不等,英文则都是1字节,但在mysql中不做这方面的设定,varchar的长度不管是中文和英文都是一格字算一个,那么。。为什么要做这个1个汉字相当于2个英文字符的设定?
回复内容:
中文在gb2312编码的时候是2字节,但在unicode编码的 情况下是1-3字节不等,英文则都是1字节,但在mysql中不做这方面的设定,varchar的长度不管是中文和英文都是一格字算一个,那么。。为什么要做这个1个汉字相当于2个英文字符的设定?
http://xfhnever.com/blog/2014/12/20/encodingformat/ 中稍微介绍了一下各种编码格式
varchar(20)指定的是字符长度
mysql中会指定表的字符集,如下面的CHARSET=gbk
:
<code> CREATE TABLE `test_type` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk COLLATE=gbk_bin </code>
为什么要做这个1个汉字相当于2个英文字符的设定?
这种设定是一般基于字符串的自然长度(也就是显示的长度), 一个汉字等于2个英文字母或数字。对于普通用户来讲就是字样,计算字节一般不都是程序和程序员干的事么。所以一般的程序设计中,一般遵循着一个汉字就是2个英文字母长度的设定,而具体占用空间则视编码和环境进行计算。
参建函数 mb_strwidth()
最早出现的编码是ASCII这样面向英文语言的编码,ASCII定义了0-127,一共128个字符的编码。对于英语这样用26个字符的,自然是够用的。问题是,如果是其他使用的字符多余26个的西方文字呢?由于已经约定了0x7F空间内的字符编码,所以我们可以看到有些西方文字,例如俄文是完全不和英文共用部分字符的,需要使用ASCII扩展集。在编码理论发展的过程,出现过各个地域分别采用不同编码体系来编码本国文字。中国是使用ISO2022体系,编码了中国的常用字符GB2312,更多的字符GBK,以及最大的集合GB18030。
通过简单的思考,你会发觉一个字节8bits只能编码类似英文这样的文字系统,而中文的常用字就有几千上万个。所以至少需要2个字节。
Unicode的出现,目的是统一全世界所有的字符编码空间,不会造成字符编码冲突问题。例如,如果你用GBK编码的字符,用UTF-8编码解释,也可能是字面有效的。Unicode编码有UTF-8,UTF-16,UTF-32,甚至UTF-7等多种格式。常说的Unicode编码是指UTF-16,因为它能以统一的方式编码几乎所有常用的字符,所以如果不是针对极端情况,我们可以假设字符都能用UTF-16表示。在UTF-16编码的情况下,汉字和英文每个字符的长度都是2个字节,是等长的。在Windows系统内部就是利用这种特性,高效的处理文字。UTF-8应用也很广泛,好处是存储上来说空间节省,解码复杂些。
MySQL在多个层面上都做了Encoding的设计,支持在不同层面上指定字符编码。
不过显然,我们应该推荐在各个层面是使用UTF-8编码。如果是内存中处理,推荐使用UTF-16,其实各种语言在设计中,好像也是这样设计的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
