码表 Unicode GBK UTF8 示例
Unicode的编码形式与对应的字符串相互转换
/*** Unicode的编码形式与对应的字符串相互转换* @author 白乾涛*/public class UnicodeUtils {public static void main(String[] args) throws UnsupportedEncodingException {test1();test2();//服务器返回的很可能是这种形式的字符串String unicodeMessages = "{\"code\":200,\"message\":\"\\u4fdd\\u5b58\\u6210\\u529f\"}";//所有的引号和反斜杠前面都要加一个反斜杠System.out.println(unicodeToString(unicodeMessages) + "\n");//{"code":200,"message":"保存成功"}}private static void test1() {System.out.println("【汉】的Unicode编码为【" + toUnicodeString('汉') + "】");//【汉】的Unicode编码为【\u6c49】System.out.println("【\\u6c49】对应的字符为【" + unicodeToString("\u6c49") + "】");//【\u6c49】对应的字符为【汉】System.out.println("\u6c49");//会自动解码【汉】System.out.println(0x9FFF - 0x3000 + "\n");//【28671】}private static void test2() {String string = "aA1:中国";String unicode = toUnicodeString(string);System.out.println("【" + string + "】的Unicode编码为【" + unicode + "】");//【\u0061\u0041\u0031\u003a\u4e2d\u56fd】System.out.println(unicodeToString(unicode));//【aA1:中国】for (int i = 0; i < unicode.length(); i += 6) {System.out.print(unicode.substring(i, i + 6));//并不会解码,只是把Unicode编码形式的字符串打印出来,为什么呢?}System.out.println("\n" + unicode);//同样,这里也不会解码。【\u0061\u0041\u0031\u003a\u4e2d\u56fd】}/*** 将【字符】转换成Unicode码形式【Unicode用两个字节来编码一个字符,2^16=16^4,所以可以用四个16进制数表示】*/public static String toUnicodeString(char c) {StringBuilder sb = new StringBuilder("");String hexString = Integer.toHexString(c);sb.append("\\u");//用于标识这是一个Unicode码for (int j = hexString.length(); j < 4; j++) {sb.append(0);//不足四位用0填充}sb.append(hexString);return sb.toString();}/*** 将【字符串】转换成Unicode码形式*/public static String toUnicodeString(String string) {StringBuilder sb = new StringBuilder("");for (int i = 0; i < string.length(); i++) {sb.append(toUnicodeString(string.charAt(i)));}return sb.toString();}/*** 将Unicode编码解析成字符串形式*/public static String unicodeToString(String uString) {StringBuilder sb = new StringBuilder();int i = -1, pos = 0;while ((i = uString.indexOf("\\u", pos)) != -1) {sb.append(uString.substring(pos, i));if (i + 5 < uString.length()) {pos = i + 6;sb.append((char) Integer.parseInt(uString.substring(i + 2, i + 6), 16));}}sb.append(uString.substring(pos));return sb.toString();}}
字符串编码格式转换
}
码表 Unicode GBK UTF8 示例的更多相关文章
- 字符编码-UNICODE,GBK,UTF-8区别【转转】
字符编码介绍及不同编码区别 今天看到这篇关于字符编码的文章,抑制不住喜悦(总结的好详细)所以转到这里来.转自:祥龙之子http://www.cnblogs.com/cy163/archive/2007 ...
- unicode gbk utf-8的差异
GB2312(1980年)定义,包含6763个汉字,682个字符 GBK1.0 定义了21003个汉字,21886个字符 ASCII->GB2312->GBK 编码方式向后兼容,即同一个字 ...
- 码表 ASCII Unicode GBK UTF-8
2017-1-3 [ASCII]一个字节(7位,128个字符,2个16进制) 不包含中文 ASCII(American Standard Code for Information Interchang ...
- UNICODE,GBK,UTF-8区别
简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...
- 【JAVA编码专题】UNICODE,GBK,UTF-8区别
简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...
- 【JAVA编码专题】UNICODE,GBK,UTF-8区别 分类: B1_JAVA 2015-02-10 21:07 153人阅读 评论(0) 收藏
简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...
- Python UNICODE GBK UTF-8 之间相互转换
Python 编码格式检测,可以使用 chardet , 例如: import urllib rawdata = urllib.urlopen('http://www.google.cn/').rea ...
- C# unicode GBK UTF-8和汉字互转
界面: 源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- UTF-8,Unicode,GBK,希腊字母读法,ASCII码表,HTTP错误码,URL编码表,HTML特殊字符,汉字编码简明对照表
UNICODE,GBK,UTF-8区别 UNICODE,GBK,UTF-8区别 简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那 ...
随机推荐
- C# 的 WCF文章 消息契约(Message Contract)在流(Stream )传输大文件中的应用
我也遇到同样问题,所以抄下做MARK http://www.cnblogs.com/lmjq/archive/2011/07/19/2110319.html 刚做完一个binding为netTcpBi ...
- Hash Killer I II
题意大概: 1.字符串hash不取模,自动溢出 构造数据卡这种hash 2.字符串hash取模1000000007,构造数据卡这种hash 题解传送门:VFleaKing http://vfleak ...
- 基于开源软件在Azure平台建立大规模系统的最佳实践
作者 王枫 发布于2014年5月28日 前言 Microsoft Azure 是微软公有云的唯一解决方案.借助这一平台,用户可以以多种方式部署和发布自己的应用. 这是一个开放的平台,除了对于Windo ...
- selenium-webdriver的等待方法
Wait commands in WebDriver Listing out the different WebDriver Wait statements that can be useful fo ...
- python进度1
Python 错误和异常 异常参数: 3.4与2.7有些不同 3.4中 try: x except NameError as e: print(type(e)) print(e) 运行结果: < ...
- 编程之美 两个叶子的节点之间 最大距离 变种 leecode
提交地址: https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 说一下思路http://www.cnblogs.com/mil ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- JavaScript高级程序设计58.pdf
15章 使用Canvas绘图 略 16章 HTML5脚本编程 HTML5规范了新的HTML标记和JavaScript API,以便简化创建动态Web界面的工作 跨文档消息传递 简称XDM,指来自不同域 ...
- GCC基本知识
掌握下面的对GCC会有一个比较清晰的大致的了解: 不经意间,GCC已发展到了4.3的版本,尽管在软件开发社区之外乏人闻问,但因为GCC在几乎所有开源软件和自由软件中都会用到,因此它的编译性能的涨落会直 ...
- hdoj 1789 Doing Homework again
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...