首页 技术 正文
技术 2022年11月15日
0 收藏 841 点赞 3,518 浏览 2413 个字

Unicode符号范围 (一个字符两个字节)     | UTF-8编码方式

(十六进制)                   | (二进制)
—————————————————————–

    这儿有四个字节
从—–00 00 00 00—到—-00 00 00 7F | 0xxxxxxx                                                      一个字符需要一个字节
从—–00 00 00 80—到—-00 00 07 FF | 110xxxxx 10xxxxxx              一个字符需要两个字节
从—–00 00 08 00—到—-00 00 FF FF | 1110xxxx 10xxxxxx 10xxxxxx          一个字符需要三个字节
从—–00 01 00 00—到—-00 10 FF FF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx   一个字符需要四个字节 

  举一个例子: It’s 知乎日报
你看到的unicode(字符集)是这样的编码表:

I 0049
t 0074
' 0027
s 0073
0020
知 77e5
乎 4e4e
日 65e5
报 62a5

每一个字符对应一个十六进制数字。

计算机只懂二进制,因此,严格按照unicode的方式(UCS-2),应该这样存储:

I 00000000 01001001
t 00000000 01110100
' 00000000 00100111
s 00000000 01110011
00000000 00100000
知 01110111 11100101
乎 01001110 01001110
日 01100101 11100101
报 01100010 10100101

这个字符串总共占用了18个字节,但是对比中英文的二进制码,可以发现,英文前9位都是0!浪费啊,浪费硬盘,浪费流量。

怎么办?

UTF。

UTF-8(编码方式)是这样做的:

1. 单字节的字符,字节的第一位设为0,对于英语文本,UTF-8码只占用一个字节,和ASCII码完全相同;

2. n个字节的字符(n>1),第一个字节的前n位设为1,第n+1位设为0,后面字节的前两位都设为10,这n个字节的其余空位填充该字符unicode码,高位用0补足。

这样就形成了如下的UTF-8标记位:

0xxxxxxx
110xxxxx 10xxxxxx
1110xxxx 10xxxxxx 10xxxxxx
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
… …

于是,”It’s 知乎日报“就变成了:

I 01001001
t 01110100
' 00100111
s 01110011
00100000
知 11100111 10011111 10100101
乎 11100100 10111001 10001110
日 11100110 10010111 10100101
报 11100110 10001010 10100101

和上边的方案对比一下,英文短了,每个中文字符却多用了一个字节。但是整个字符串只用了17个字节,比上边的18个短了一点点。

下边是课后作业:

请将”It’s 知乎日报“的GB2312和GBK码(自行google)转成二进制。不考虑历史因素,从技术角度解释为什么在unicode和UTF-8大行其道的同时,GB2312和GBK仍在广泛使用。

剧透:一切都是为了节省你的硬盘和流量。

做个简单的比喻, unicode相当于中文, UTF-8, UTF-16等相当于 行书, 楷书, 草书等各种书写方式.     

例子一:
String s =”abc中文喆镕”;
byte[] a;
a=s.getBytes();
System.out.println(“默认:”+Arrays.toString(a));
a=s.getBytes(“GBK”);
System.out.println(“GBK:”+Arrays.toString(a));
a=s.getBytes(“UTF-8”);
System.out.println(“UTF-8:”+Arrays.toString(a));
输出:
默认:[97, 98, 99, -42, -48, -50, -60, -122, -76, -23, 70]
GBK:[97, 98, 99, -42, -48, -50, -60, -122, -76, -23, 70]
UTF-8:[97, 98, 99, -28, -72, -83, -26, -106, -121, -27, -106, -122, -23, -107, -107]

例子二:
String s;
byte[] a;
a=new byte[]{97, 98, 99, -42, -48, -50, -60, -122, -76, -23, 70};
s=new String(a);
System.out.println(s);
a=new byte[]{97, 98, 99, -42, -48, -50, -60, -122, -76, -23, 70};
s=new String(a,”GBK”);
System.out.println(s);
a=new byte[]{97, 98, 99, -42, -48, -50, -60, -122, -76, -23, 70};
s=new String(a,”UTF-8″);
System.out.println(s);
a=new byte[]{97, 98, 99, -28, -72, -83, -26, -106, -121, -27, -106, -122, -23, -107, -107};
s=new String(a,”UTF-8″);
System.out.println(s);
输出:
abc中文喆镕
abc中文喆镕
abc??????
abc中文喆镕

    Unicode 与 GBK 是两个完全不样的字符编码方案, 其两者没有直接关系, 要对其进行相
互转换, 最直接最高效的方法是查表.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,026
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,517
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,364
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,145
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,779
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,856