Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46349
  • 博文数量: 10
  • 博客积分: 1070
  • 博客等级: 少尉
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-26 13:44
文章分类

全部博文(10)

文章存档

2010年(8)

2009年(2)

我的朋友
最近访客

分类: Java

2009-04-27 10:09:28

public class Test {
 public static void main(String[] args) {
  String s = "简介";
  String tt = gbEncoding(s);
  System.out.println(decodeUnicode(tt));
 }

 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 + "" + hexB;
  }
  System.out.println("unicodeBytes is: " + unicodeBytes);
  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("", 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();
 }
}
 
出处:http://ekumen.javaeye.com/blog/265613
阅读(514) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~