java 中文转换成Unicode编码和Unicode编码转换成中文
转自:一叶飘舟
http://blog.csdn.net/jdsjlzx/article/details/
package lia.meetlucene; import java.io.IOException;
import org.apache.lucene.index.CorruptIndexException; public class Unicode {
public static void main(String[] args) throws CorruptIndexException,
IOException {
String s = "简介";
String tt = gbEncoding(s); // String tt1 = "你好,我想给你说一个事情";
System.out.println("unicodeBytes is: " + tt);
// 输出“简介”的unicode编码
System.out.println("对应的中文: " + decodeUnicode("\\u7b80\\u4ecb")); // System.out.println(decodeUnicode(tt1));
// 输出unicode编码对应的中文
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(s.indexOf("\\"));
} public static String gbEncoding(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
} public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
} }
代码详解:
public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 1);
//使得第一个unicode在start~end之间,+1,+2,+3均可
System.out.println(start + "asdfasd~~~~~~~~~~~~~~~~~~~~~``" + end);
// the index of the first occurrence of the specified substring,
// starting at the specified index,
// or -1 if there is no such occurrence.
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = 0;
if (charStr.length() == 4) {
letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
}
//防止出错
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
java 中文转换成Unicode编码和Unicode编码转换成中文的更多相关文章
- 中文转换成Unicode编码 和 Unicode编码转换为中文
前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为Unicode编码,然后再解码成中文,以下是实现的过程,非常简单! p ...
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...
- 转换编码,将Unicode编码转换成可以浏览的utf-8编码
//转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...
- 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念
转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...
- 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...
- 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
我们最初学习计算机的时候,都学过ASCII编码. 但是为了表示各种各样的语言,在计算机技术的发展过程中,逐渐出现了很多不同标准的编码格式, 重要的有Unicode.UTF.ISO-8859-1和中国人 ...
- JAVA字符编码二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换
第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 1.函数介绍 在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有 ...
- 字符编码知识:Unicode、UTF-8、ASCII、GB2312等编码之间是如何转换的?
转自: http://apps.hi.baidu.com/share/detail/17798660 字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得字符编码的知识.不注意的人可能对这个不 ...
- unicode编码、字符的转换和得到汉字的区位码
一:unicode编码.字符的转换截图 二:unicode编码.字符的转换代码 using System; using System.Collections.Generic; using System ...
随机推荐
- HTML页面表单输入框去掉鼠标选中后边框变色的效果
标题说的有些含糊,实在不知道怎么描述了,就简单说一下吧,一个最简单的表单,代码如下,没有任何样式和名字, <!DOCTYPE html> <html> <head> ...
- Javascript备忘
js输出对象类型: Object.prototype.toString.apply(s) 设置单行点击效果: obj.style.background = "#efefef";se ...
- C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)
#include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...
- C语言 给字符数组赋值的方法
typedef struct _tagTESTCHAR { char szTest[30];}TESTCHAR , *PTESTCHAR; int main(int argc, char* argv[ ...
- Git自动部署
Git自动部署文件位于repository下面的hooks里的post-receive #!/bin/sh set -e git-update-server-info gitosis-run-hook ...
- Java Hour 36 Weathre ( 9 ) struts2 – exception
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 35 Exception Handling 直接添加全局性的异常处理 ...
- sybase常用SQL语句,工作中积累的
-------创建sybase设备 语句--------- disk init name="DEV_DB_CLIENT_DAT26", physname="F:\syba ...
- 好用的PHP分页类
<?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; ...
- Android:dimen尺寸资源文件的使用
dimen.xml在values文件夹下面 <resources> <!-- Default screen margins, per the Android Design guide ...
- 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。
现象: 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序.