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 ...
随机推荐
- iOS 和Android中的正则表达式简单使用
ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...
- 【USACO】beads
题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...
- MFC dfs遍历文件
//如果涉及到大文件的遍历(大于4GB),可以将以下代码_finddata_t换成__finddata64_t,_findfirst换成_findfirst64,_findnext换成_findnex ...
- 最新php环境搭建
参考文章:http://jingyan.baidu.com/article/29697b912f6539ab20de3cf8.html
- SQL Server 2012 OFFSET/FETCH NEXT分页示例(转载)
原文:http://beyondrelational.com/modules/29/presentations/483/scripts/12983/sql-server-2012-server-sid ...
- js 事件监听 兼容浏览器
js 事件监听 兼容浏览器 ie 用 attachEvent w3c(firefox/chrome) 用 addEventListener 删除事件监听 ie 用 detachEven ...
- hdu 4398 STL
题意描述半天描述不好,直接粘贴了 Now your team is participating a programming contest whose rules are slightly diffe ...
- &是什么运算符(转)
&表示两种运算符,其中一种表示取值运算符,一种是按位与 取值运算符 int a=1; int *p=&a; //其中&a表示的就是把a中的地址取出来,然后赋给指针变量,也就是说 ...
- ☆ fragment和fragmentactivity解析 (转)
一.为什么要使用Fragment 1.当我们需要动态的多界面切换的时候,就需要将UI元素和Activity融合成一 个模块.在2.3中我们一般通过各种Activity中进行跳转来实现多界面的跳转和单 ...
- HDU 4341 分组背包
B - Gold miner Time Limit:2000MS Memory Limit:32768KB Description Homelesser likes playing ...