论坛首页 Java企业应用论坛

java中GB2312 To Utf-8字符转换

浏览 19179 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2007-08-08  

    朋友让我帮他写个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. }  
   发表时间:2007-08-08  
参见java.util.Properties。
0 请登录后投票
   发表时间:2007-08-08  
引用
参见java.util.Properties。

呵呵,刚才看来一下Properties的源码,早看了就少费点劲了^_^
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics