重新认识unicode跟utf8编码
重新认识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
- 以前也没怎么注意这个问题,因为一般情况下我都让前后端编码统一,不过博主分析的挺好,赞!

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

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Unicode is a character encoding standard used to represent various languages and symbols. To convert Unicode encoding to Chinese characters, you can use Python's built-in functions chr() and ord().

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Are you troubled by Chinese garbled characters in Eclipse? To try these solutions, you need specific code examples 1. Background introduction With the continuous development of computer technology, Chinese plays an increasingly important role in software development. However, many developers encounter garbled code problems when using Eclipse for Chinese development, which affects work efficiency. Then, this article will introduce some common garbled code problems and give corresponding solutions and code examples to help readers solve the Chinese garbled code problem in Eclipse. 2. Common garbled code problems and solution files

JSON (JavaScriptObjectNotation) is a lightweight data exchange format commonly used for data exchange between web applications. When processing JSON data, we often encounter Unicode-encoded Chinese characters (such as "u4e2du6587") and need to convert them into readable Chinese characters. In PHP, we can achieve this conversion through some simple methods. Next, we will detail how to convert JSONUnico
