重新认识unicode和utf8编码
重新认识unicode和utf8编码
直到今天,准确的说是刚才,我才知道UTF-8编码和Unicode编码是不一样的,是有区别的囧
他们之间是有一定的联系的,看看他们的区别:
<code>UTF-8的长度是不一定的,有可能是1、2、3字节Unicode长度一定,2个字节(USC-2)UTF-8可以和Unicode互相转换</code>
unicode和utf8的关系
Unicode(16进制) | UTF-8(二进制) |
---|---|
0000 - 007F | 0xxxxxxx |
0080 - 07FF | 110xxxxx 10xxxxxx |
0800 - FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
上面的表格有2个意思,第一个显而易见就是说Unicode和UTF-8字符范围的对应,还有一个可以看出Unicode怎么和UTF-8互相转换:
先说UTF-8到Unicode的转换
UTF-8编码的二进制和上面的3种格式进行匹配,匹配到之后去掉固定位(表格中的非x位置),然后从右到左每8位一组,不够8位左边不领,凑够2个字节16 bits,这16 bits所表示的就是UTF-8对应的Unicode编码,看看下面几个例子:
上面图片中的文字编码格式为UTF-8,可以用WinHex看到其16进制表示
<code>字符 => UTF-8 => UTF-8二进制=> 去掉固定位置凑够16位的二进制 => 16进制汉 => E6B189 => 11100110 10110001 10001001 => 01101100 01001001 => 6C49字 => E5AD97 => 11100101 10101101 10010111 => 01011011 01010111 => 5B57#下面是在chrome命令行下面运行的结果'\u6C49'"汉"'\u5B57'"字"#到这里的话,从UTF-8转换到Unicode已经是一件非常容易的事了,看看转换的伪代码读取一个字节,11100110判断该UTF-8字符的格式,属于第三种,3个字节继续读取2个字节得到 11100101 10101101 10010111按照格式去掉固定位 1011011 01010111不够16位,左边补零 01011011 01010111 => 5B57</code>
再看看从Unicode到UTF-8的转换
<code>5B57获取5B57所在的Unicode范围,0800 </code>
说说问题
再说说今天这个问题的起因,从前端输入很多单词,UTF-8格式每个词最多30个字节,因此会在前端和后台分别做验证,javascript用的是Unicode编码,后端程序用的是UTF-8编码,现在的解决办法是这样
前端
<code>function utf8_bytes(str){ var len = 0, unicode; for(var i = 0; i </code>
后台
<code>#对于GBK字符串$len = ceil(strlen(bin2hex(iconv('GBK', 'UTF-8', $word)))/2);#对于UTF8字符串$len = ceil(strlen(bin2hex($word))/2);</code>
5/21/2015 8:21:53 PM
本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
- 4楼清香白莲素还真
- Unicode是字符集,定义每个字符对应的数字。,UTF-8、UTF-16等是编码格式,定义“字符对应的数字”如何以二进制的方式存储。
- Re: 奔跑的Man
- @清香白莲素还真,我觉得这样说更好点:,Uincode定义每个数字(0x0000~0xFFFF)对应的字符,,UTF-8是定义字符对应的数字(不是所有的数字都有对应的字符),,完了之后他们之间有个映射关系
- 3楼穆恩
- 第一段有错别字
- Re: 奔跑的Man
- @穆恩,已改,太粗心了,这里检讨下,谢谢提醒
- 2楼小丸
- 博主所说的Unicode其实是utf-16
- 1楼upfriend
- 以前也没怎么注意这个问题,因为一般情况下我都让前后端编码统一,不过博主分析的挺好,赞!

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 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.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

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.

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 makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

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 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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.