转自:一叶飘舟
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编码转换成中文的更多相关文章

  1. 中文转换成Unicode编码 和 Unicode编码转换为中文

    前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为Unicode编码,然后再解码成中文,以下是实现的过程,非常简单! p ...

  2. 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)

    Unicode与UTF-8互转(C语言实现):http://blog.csdn.net/tge7618291/article/details/7599902 汉字 Unicode 编码范围:http: ...

  3. 转换编码,将Unicode编码转换成可以浏览的utf-8编码

    //转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...

  4. 从Java String实例来理解ANSI、Unicode、BMP、UTF等编码概念

    转(http://www.codeceo.com/article/java-string-ansi-unicode-bmp-utf.html#0-tsina-1-10971-397232819ff9a ...

  5. 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...

  6. 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换

    我们最初学习计算机的时候,都学过ASCII编码. 但是为了表示各种各样的语言,在计算机技术的发展过程中,逐渐出现了很多不同标准的编码格式, 重要的有Unicode.UTF.ISO-8859-1和中国人 ...

  7. JAVA字符编码二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换   1.函数介绍 在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有 ...

  8. 字符编码知识:Unicode、UTF-8、ASCII、GB2312等编码之间是如何转换的?

    转自:  http://apps.hi.baidu.com/share/detail/17798660 字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得字符编码的知识.不注意的人可能对这个不 ...

  9. unicode编码、字符的转换和得到汉字的区位码

    一:unicode编码.字符的转换截图 二:unicode编码.字符的转换代码 using System; using System.Collections.Generic; using System ...

随机推荐

  1. 使用 TRegistry 类[1]: 显示各主键下的项

    使用 TRegistry 类[1]: 显示各主键下的项 {XP 注册表中的主键} HKEY_CLASSES_ROOT    {文件类型信息} HKEY_CURRENT_USER    {当前用户信息} ...

  2. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  3. Greedy:Allowance(POJ 3040)

    零用钱大作战 题目大意:农夫和牛又搞新花样了,现在农夫想给Bessie每个星期都给一点零用钱,农夫有一堆面值的钱币,并且这个钱币都能被上一个钱币整除(1,5,10,50),并且钱币有一定数量,要你求最 ...

  4. js简单上传进度条

    效果如下:

  5. 解决 mysql 启动报错--发现系统错误2,系统找不到指定的文件

    HKEY_LOCAL_MACHINE-SYSTEM-CurrentControlSet-services-mysql(服务名)-ImagePath 更改为(自己的):"C:\Program ...

  6. 获取Assets目录下的图片显示并上传

    package com.jingle.getlocal; import java.io.ByteArrayOutputStream; import java.io.InputStream; impor ...

  7. Android 如何让EditText不自动获取焦点

    解决之道:在EditText的父级控件中找一个,设置成 android:focusable="true"     android:focusableInTouchMode=&quo ...

  8. 【vijos1659】河蟹王国 线段树<区间修改+区间最大值>

    描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...

  9. 关于P,V操作理解的分析

      P-V操作定义: 假设sem是个整型变量. P原语的主要操作是: (1)sem减1: (2)若sem减1后仍大于或等于零,则该进程继续执行: (3)若sem减1后小于零,则该进程被阻塞,在相应队列 ...

  10. centOS下安装node4.x

    在以前 Node 0.x 的时候,只需要通过 yum install -y nodejs 就可以把 Node 安装好了.但是 yum 安装的 Node 只能安装到 0.10.40 版的,并不能安装到 ...