中文转unicode,中文转bytes,unicode转bytes java实现
字符串的编码格式: String hex="u5df2u5b9eu540du5236";// 十六进制
相互转换的代码 1.十进制转unicode编码 =s.split(";"=( i=0;i
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sbf.toString();
} 2.unicode转char数组 StringBuffer string </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuffer();
String[] hex </span>= unicode.split("\u"<span style="color: #000000;">);
</span><span style="color: #0000ff;">char</span>[] cs=<span style="color: #0000ff;">new</span> <span style="color: #0000ff;">char</span><span style="color: #000000;">[hex.length];
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = 1; i < hex.length; i++<span style="color: #000000;">) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 转换出每一个代码点 </span>
<span style="color: #0000ff;">int</span> data = Integer.parseInt(hex[i],16<span style="color: #000000;">);
cs[i]</span>=(<span style="color: #0000ff;">char</span><span style="color: #000000;">) data;
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> cs;
} 3.汉字转unicode
= = "" ( i = 0; i < s.length(); i++= Integer.toHexString(s.charAt(i) & 0xffff= s1 + "u" +
utf-8 utf-8格式的中文由三位字节组成。 UTF-8的编码规则很简单,只有二条: 1)对于单字节的符号,字节的第一位设为0,后面7位为这个符号的unicode码。因此对于英语字母,UTF-8编码和ASCII码是相同的。 2)对于n字节的符号(n>1),第一个字节的前n位都设为1,第n+1位设为0,后面字节的前两位一律设为10。剩下的没有提及的二进制位,全部为这个符号的unicode码。 下表总结了编码规则,字母x表示可用编码的位。 Unicode符号范围 | UTF-8编码方式 (十六进制) | (二进制) --------------------+--------------------------------------------- 0000 0000-0000 007F | 0xxxxxxx 0000 0080-0000 07FF | 110xxxxx 10xxxxxx 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 1.中文转unicode = = "" ( i = 0; i < s.length(); i++= Integer.toHexString(s.charAt(i) & 0xffff= s1 + "u" +
2.中文转bytes [] b=s.getBytes("utf-8");
3. unicode转utf-8
String unicode2utf8(
lowByte = input & 0x00ff highByte = (input & 0xff00) >>> 8
</span><span style="color: #008000;">//</span><span style="color: #008000;"> UTF-8的第1个字节是1110 + highByte高4位</span>
<span style="color: #0000ff;">int</span> high4inHighByte = (highByte& 0xf0) >>> 4<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> utf8Byte1 = (7 << 5) +<span style="color: #000000;"> high4inHighByte;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> UTF-8的第2个字节是10 + highByte低4位 + lowByte高2位</span>
<span style="color: #0000ff;">int</span> low4inHighByte = highByte & 0x0f<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> high2inLowByte = (lowByte& 0xc0) >>> 6<span style="color: #000000;">;
</span><span style="color: #0000ff;">int</span> utf8Byte2 = (1 << 7) + (low4inHighByte << 2) +<span style="color: #000000;"> high2inLowByte;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> UTF-8的第3个字节是10 + lowByte低6位</span>
<span style="color: #0000ff;">int</span> utf8Byte3 = (1 << 7) + (lowByte & 0x3f<span style="color: #000000;">);
String result </span>= Integer.toHexString(utf8Byte1) + "," + Integer.toHexString(utf8Byte2) + ","
+<span style="color: #000000;"> Integer.toHexString(utf8Byte3);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
}</span></pre>
gbk GBK编码,是对GB2312编码的扩展,因此完全兼容GB2312-80标准。GBK编码依然采用双字节编码方案,其编码范围:8140-FEFE,剔除xx7F码位,共23940个码位。共收录汉字和图形符号21886个,其中汉字(包括部首和构件)21003个,图形符号883个。GBK编码支持国际标准ISO/IEC10646-1和国家标准GB13000-1中的全部中日韩汉字,并包含了BIG5编码中的所有汉字。GBK编码方案于1995年12月15日正式发布,这一版的GBK规范为1.0版。 GBK 亦采用双字节表示,总体编码范围为 8140-FEFE,首字节在 81-FE 之间,尾字节在 40-FE 之间,剔除 xx7F 一条线。总计 23940 个码位,共收入 21886 个汉字和图形符号,其中汉字(包括部首和构件)21003 个,图形符号 883 个。 全部编码分为三大部分: 1. 汉字区。包括: a. GB 2312 汉字区。即 GBK/2: B0A1-F7FE。收录 GB 2312 汉字 6763 个,按原顺序排列。 b. GB 13000.1 扩充汉字区。包括: (1) GBK/3: 8140-A0FE。收录 GB 13000.1 中的 CJK 汉字 6080 个。 (2) GBK/4: AA40-FEA0。收录 CJK 汉字和增补的汉字 8160 个。CJK 汉字在前,按 UCS 代码大小排列;增补的汉字(包括部首和构件)在后,按《康熙字典》的页码/字位排列。 (3) 汉字“〇”安排在图形符号区GBK/5:A996。 2. 图形符号区。包括: a. GB 2312 非汉字符号区。即 GBK/1: A1A1-A9FE。其中除 GB 2312 的符号外,还有 10 个小写罗马数字和 GB 12345 增补的符号。计符号 717 个。 b. GB 13000.1 扩充非汉字区。即 GBK/5: A840-A9A0。BIG-5 非汉字符号、结构符和“〇”排列在此区。计符号 166 个。 3. 用户自定义区:分为(1)(2)(3)三个小区。 (1) AAA1-AFFE,码位 564 个。 (2) F8A1-FEFE,码位 658 个。 (3) A140-A7A0,码位 672 个。 第(3)区尽管对用户开放,但限制使用,因为不排除未来在此区域增补新字符的可能性。 举例: String s="中文"[] b=s.getBytes("gbk");
bytes为 [-42,-48,-50,-60] 补码为: 【214,208,206,196】 转换为十六进展为 【D6,D0,CE,C4】 按照gbk表去查询汉字 编码完全匹配。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |