java中GB2312 To Utf-8字符转换

    朋友让我帮他写个gb2312->utf-8的字符转换程序,找了半天没有在网上找到合适的,于是自己动手写了一个,呵呵。把它贴在这里,免得以后忘记了 ^_^

    实现思路大致如下:

  •  取得一个汉字的Unicode码
  • 把Unicode码分解为两个16进制数据字符串(丢弃前两个字节)
  • 把这两个16进制数据字符串转换成二进制数据字符串
  • 把二进制数据字符串分解为三个串,第一个串为4(0~4)个位,在高位加上标记位“1110”,第二(4~10)、三个(10~16)串均为6个位,分别在高位加上“10”标记位
  • 把这三个二进制串分别转换为10进制数据并赋值给字节型数组
  • 根据这个字节型数组构造UTF-8字符
java 代码
 
  1. import java.io.File;   
  2. import java.io.FileOutputStream;   
  3. import java.io.UnsupportedEncodingException;   
  4.   
  5. /**  
  6.  * 2007-8-10 jyin at gomez dot com  
  7.  */  
  8. public class CharsetConvertor {   
  9.     public static void main(String[] args) {   
  10.         String str = "This is a test for *中网!@#$。,?";   
  11.         try {   
  12.             File f = new File("D:/test.txt");   
  13.             FileOutputStream fio = new FileOutputStream(f);   
  14.             String s = gbToUtf8(str);   
  15.             fio.write(s.getBytes("UTF-8"));   
  16.             fio.close();   
  17.         }   
  18.         catch (Exception e) {   
  19.             e.printStackTrace();   
  20.         }   
  21.     }   
  22.   
  23.     public static String gbToUtf8(String str) throws UnsupportedEncodingException {   
  24.         StringBuffer sb = new StringBuffer();   
  25.         for (int i = 0; i < str.length(); i++) {   
  26.             String s = str.substring(i, i + 1);   
  27.             if (s.charAt(0) > 0x80) {   
  28.                 byte[] bytes = s.getBytes("Unicode");   
  29.                 String binaryStr = "";   
  30.                 for (int j = 2; j < bytes.length; j += 2) {   
  31.                     // the first byte   
  32.                     String hexStr = getHexString(bytes[j + 1]);   
  33.                     String binStr = getBinaryString(Integer.valueOf(hexStr, 16));   
  34.                     binaryStr += binStr;   
  35.                     // the second byte   
  36.                     hexStr = getHexString(bytes[j]);   
  37.                     binStr = getBinaryString(Integer.valueOf(hexStr, 16));   
  38.                     binaryStr += binStr;   
  39.                 }   
  40.                 // convert unicode to utf-8   
  41.                 String s1 = "1110" + binaryStr.substring(04);   
  42.                 String s2 = "10" + binaryStr.substring(410);   
  43.                 String s3 = "10" + binaryStr.substring(1016);   
  44.                 byte[] bs = new byte[3];   
  45.                 bs[0] = Integer.valueOf(s1, 2).byteValue();   
  46.                 bs[1] = Integer.valueOf(s2, 2).byteValue();   
  47.                 bs[2] = Integer.valueOf(s3, 2).byteValue();   
  48.                 String ss = new String(bs, "UTF-8");   
  49.                 sb.append(ss);   
  50.             } else {   
  51.                 sb.append(s);   
  52.             }   
  53.         }   
  54.         return sb.toString();   
  55.     }   
  56.   
  57.     private static String getHexString(byte b) {   
  58.         String hexStr = Integer.toHexString(b);   
  59.         int m = hexStr.length();   
  60.         if (m < 2) {   
  61.             hexStr = "0" + hexStr;   
  62.         } else {   
  63.             hexStr = hexStr.substring(m - 2);   
  64.         }   
  65.         return hexStr;   
  66.     }   
  67.   
  68.     private static String getBinaryString(int i) {   
  69.         String binaryStr = Integer.toBinaryString(i);   
  70.         int length = binaryStr.length();   
  71.         for (int l = 0; l < 8 - length; l++) {   
  72.             binaryStr = "0" + binaryStr;   
  73.         }   
  74.         return binaryStr;   
  75.     }   
  76. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值