GBK2UTF8

Java代码   收藏代码
  1. package com.elmer.c;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. public class GbktoUtf8 {  
  6.       
  7.        public String convertString(String gbk){  
  8.             String utf8 = "";  
  9.             try {  
  10.                 utf8 = new String(gbk2utf8(gbk),"UTF-8");  
  11.             } catch (UnsupportedEncodingException e) {  
  12.                 e.printStackTrace();  
  13.             }  
  14.             return utf8;  
  15.         }  
  16.   
  17.         public byte[] gbk2utf8(String chenese) {  
  18.             char c[] = chenese.toCharArray();  
  19.             byte[] fullByte = new byte[3 * c.length];  
  20.             for (int i = 0; i < c.length; i++) {  
  21.                 int m = (int) c[i];  
  22.                 String word = Integer.toBinaryString(m);  
  23.   
  24.                 StringBuffer sb = new StringBuffer();  
  25.                 int len = 16 - word.length();  
  26.                 for (int j = 0; j < len; j++) {  
  27.                     sb.append("0");  
  28.                 }  
  29.                 sb.append(word);  
  30.                 sb.insert(0"1110");  
  31.                 sb.insert(8"10");  
  32.                 sb.insert(16"10");  
  33.   
  34.                 String s1 = sb.substring(08);  
  35.                 String s2 = sb.substring(816);  
  36.                 String s3 = sb.substring(16);  
  37.   
  38.                 byte b0 = Integer.valueOf(s1, 2).byteValue();  
  39.                 byte b1 = Integer.valueOf(s2, 2).byteValue();  
  40.                 byte b2 = Integer.valueOf(s3, 2).byteValue();  
  41.                 byte[] bf = new byte[3];  
  42.                 bf[0] = b0;  
  43.                 fullByte[i * 3] = bf[0];  
  44.                 bf[1] = b1;  
  45.                 fullByte[i * 3 + 1] = bf[1];  
  46.                 bf[2] = b2;  
  47.                 fullByte[i * 3 + 2] = bf[2];  
  48.   
  49.             }  
  50.             return fullByte;  
  51.         }  
  52.   
  53.         public static void main(String[] args) {  
  54.             GbktoUtf8 gtu = new GbktoUtf8();  
  55.             System.out.println(gtu.convertString("我是q !"));  
  56.         }  
  57.   
  58. }  

核心是找的资料,还没放到web里试过 


二. 
Java代码   收藏代码
  1. public class TranCharset {  
  2.   
  3.     private static final String PRE_FIX_UTF = "&#x";  
  4.   
  5.     private static final String POS_FIX_UTF = ";";  
  6.   
  7.     public TranCharset() {  
  8.     }  
  9.   
  10.     /** 
  11.      * Translate charset encoding to unicode 
  12.      *  
  13.      * @param sTemp 
  14.      *            charset encoding is gb2312 
  15.      * @return charset encoding is unicode 
  16.      */  
  17.     public static String XmlFormalize(String sTemp) {  
  18.         StringBuffer sb = new StringBuffer();  
  19.   
  20.         if (sTemp == null || sTemp.equals("")) {  
  21.             return "";  
  22.         }  
  23.         String s = TranCharset.TranEncodeTOGB(sTemp);  
  24.         for (int i = 0; i < s.length(); i++) {  
  25.             char cChar = s.charAt(i);  
  26.             if (TranCharset.isGB2312(cChar)) {  
  27.                 sb.append(PRE_FIX_UTF);  
  28.                 sb.append(Integer.toHexString(cChar));  
  29.                 sb.append(POS_FIX_UTF);  
  30.             } else {  
  31.                 switch ((int) cChar) {  
  32.                 case 32:  
  33.                     sb.append("&#32;");  
  34.                     break;  
  35.                 case 34:  
  36.                     sb.append("&quot;");  
  37.                     break;  
  38.                 case 38:  
  39.                     sb.append("&amp;");  
  40.                     break;  
  41.                 case 60:  
  42.                     sb.append("&lt;");  
  43.                     break;  
  44.                 case 62:  
  45.                     sb.append("&gt;");  
  46.                     break;  
  47.                 default:  
  48.                     sb.append(cChar);  
  49.                 }  
  50.             }  
  51.         }  
  52.         return sb.toString();  
  53.     }  
  54.   
  55.     /** 
  56.      * 将字符串编码格式转成GB2312 
  57.      *  
  58.      * @param str 
  59.      * @return 
  60.      */  
  61.     public static String TranEncodeTOGB(String str) {  
  62.         try {  
  63.             String strEncode = TranCharset.getEncoding(str);  
  64.             String temp = new String(str.getBytes(strEncode), "GB2312");  
  65.             return temp;  
  66.         } catch (java.io.IOException ex) {  
  67.   
  68.             return null;  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      * 判断输入字符是否为gb2312的编码格式 
  74.      *  
  75.      * @param c 
  76.      *            输入字符 
  77.      * @return 如果是gb2312返回真,否则返回假 
  78.      */  
  79.     public static boolean isGB2312(char c) {  
  80.         Character ch = new Character(c);  
  81.         String sCh = ch.toString();  
  82.         try {  
  83.             byte[] bb = sCh.getBytes("gb2312");  
  84.             if (bb.length > 1) {  
  85.                 return true;  
  86.             }  
  87.         } catch (java.io.UnsupportedEncodingException ex) {  
  88.             return false;  
  89.         }  
  90.         return false;  
  91.     }  
  92.   
  93.     /** 
  94.      * 判断字符串的编码 
  95.      *  
  96.      * @param str 
  97.      * @return 
  98.      */  
  99.     public static String getEncoding(String str) {  
  100.         String encode = "GB2312";  
  101.         try {  
  102.             if (str.equals(new String(str.getBytes(encode), encode))) {  
  103.                 String s = encode;  
  104.                 return s;  
  105.             }  
  106.         } catch (Exception exception) {  
  107.         }  
  108.         encode = "ISO-8859-1";  
  109.         try {  
  110.             if (str.equals(new String(str.getBytes(encode), encode))) {  
  111.                 String s1 = encode;  
  112.                 return s1;  
  113.             }  
  114.         } catch (Exception exception1) {  
  115.         }  
  116.         encode = "UTF-8";  
  117.         try {  
  118.             if (str.equals(new String(str.getBytes(encode), encode))) {  
  119.                 String s2 = encode;  
  120.                 return s2;  
  121.             }  
  122.         } catch (Exception exception2) {  
  123.         }  
  124.         encode = "GBK";  
  125.         try {  
  126.             if (str.equals(new String(str.getBytes(encode), encode))) {  
  127.                 String s3 = encode;  
  128.                 return s3;  
  129.             }  
  130.         } catch (Exception exception3) {  
  131.         }  
  132.         encode = "BIG5";  
  133.         try {  
  134.             if (str.equals(new String(str.getBytes(encode), encode))) {  
  135.                 String s4 = encode;  
  136.                 return s4;  
  137.             }  
  138.         } catch (Exception exception3) {  
  139.         }  
  140.         return "";  
  141.     }  
  142.   
  143.     public static void main(String args[]) {  
  144.         System.out.println(XmlFormalize("下载"));  
  145.     }  
  146. }  


Java代码   收藏代码
  1. package com.lang.string;  
  2.   
  3.   
  4. public class ConverFromGBKToUTF8 {  
  5.  public static void main(String[] args){  
  6.     
  7.   try {  
  8.      
  9.          ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();  
  10.          String chenese = "我是我我们";  
  11.         byte [] fullByte = convert.gbk2utf8(chenese);  
  12.          String fullStr = new String(fullByte, "UTF-8");  
  13.          System.out.println("string from GBK to UTF-8 byte:  " + fullStr);  
  14.   
  15.   
  16.      } catch (Exception e) {  
  17.       e.printStackTrace();  
  18.      }  
  19.  }  
  20.    
  21.  public byte[] gbk2utf8(String chenese){  
  22.   char c[] = chenese.toCharArray();  
  23.         byte [] fullByte =new byte[3*c.length];  
  24.         for(int i=0; i<c.length; i++){  
  25.          int m = (int)c[i];  
  26.          String word = Integer.toBinaryString(m);  
  27. //         System.out.println(word);  
  28.           
  29.          StringBuffer sb = new StringBuffer();  
  30.          int len = 16 - word.length();  
  31.          //补零  
  32.          for(int j=0; j<len; j++){  
  33.           sb.append("0");  
  34.          }  
  35.          sb.append(word);  
  36.          sb.insert(0"1110");  
  37.          sb.insert(8"10");  
  38.          sb.insert(16"10");  
  39.            
  40. //         System.out.println(sb.toString());  
  41.            
  42.          String s1 = sb.substring(08);            
  43.          String s2 = sb.substring(816);            
  44.          String s3 = sb.substring(16);  
  45.            
  46.          byte b0 = Integer.valueOf(s1, 2).byteValue();  
  47.          byte b1 = Integer.valueOf(s2, 2).byteValue();  
  48.          byte b2 = Integer.valueOf(s3, 2).byteValue();  
  49.          byte[] bf = new byte[3];  
  50.          bf[0] = b0;  
  51.          fullByte[i*3] = bf[0];  
  52.          bf[1] = b1;  
  53.          fullByte[i*3+1] = bf[1];  
  54.          bf[2] = b2;  
  55.          fullByte[i*3+2] = bf[2];  
  56.            
  57.         }  
  58.         return fullByte;  
  59.  }  
  60. }  



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

1、函数介绍 
在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两个主要函数为: 
1)将字符串用指定的编码集合解析成字节数组,完成Unicode-〉charsetName转换 
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException  

2)将字节数组以指定的编码集合构造成字符串,完成charsetName-〉Unicode转换 
public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException 

2、Unicode与各编码之间的直接转换 
下面以对中文字符串"a中文"的编码转换为例,来了解各种编码之间的转换 
1)Unicode和GBK 
测试结果如下,每个汉字转换为两个字节,且是可逆的,即通过字节可以转换回字符串 
String-GBK〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4 
ByteArray-GBK〉String:0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u4E2D\u6587(a中文) 

2)Unicode和UTF-8 
测试结果如下,每个汉字转换为三个字节,且是可逆的,即通过字节可以转换回字符串 
String-UTF-8〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87 
ByteArray-UTF-8〉String:0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u4E2D\u6587(a中文) 
3)Unicode和ISO-8859-1 
测试结果如下,当存在汉字时转换失败,非可逆,即通过字节不能再转换回字符串 
String-ISO-8859-1〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0x3F 0x3F 
ByteArray-ISO-8859-1〉String:0x61 0x3F 0x3F-〉\u0061\u003F\u003F(a??) 
3、Unicode与各编码之间的交叉转换 
在上面直接转换中,由字符串(Unicode)生成的字节数组,在构造回字符串时,使用的是正确的编码集合,如果使用的不是正确的编码集合会怎样呢?会正确构造吗?如果不能正确构造能有办法恢复吗?会信息丢失吗? 

下面我们就来看看这种情况,这部分可以说明在某些情况下虽然我们最终正确显示了结果,但其间仍然进行了不正确的转换。 

1)能够正确显示的中间不正确转换 
我们知道String-GBK〉ByteArray-GBK〉String是正确的,但如果我们采用String-GBK〉ByteArray-ISO-8859-1〉String呢?通过测试结果如下: 
String-GBK〉ByteArray-ISO-8859-1〉String:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u00D6\u00D0\u00CE\u00C4(a????) 

这时我们得到的字符串为?乱码“a????”,但是通过继续转换我们仍然可以复原回正确的字符串“a中文”,过程如下: 
String-GBK〉ByteArray-ISO-8859-1〉String-ISO-8859-1〉ByteArray-GBK〉String 
对应:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u00D6\u00D0\u00CE\u00C4(a????)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u4E2D\u6587(a中文) 

也就是我们在首次构造字符串时,我们用了错误的编码集合得到了错误的乱码,但是我们通过错上加错,再用错误的编码集合获取字节数组,然后再用正确的编码集合构造,就又恢复了正确的字符串。这时就属于是“能够正确显示的中间不正确转换”。在Jsp页面提交数据处理时常常发生这种情况。 

此外能够正确显示的中间不正确转换还有: 
String-UTF-8〉ByteArray-ISO-8859-1〉String-ISO-8859-1〉ByteArray-UTF-8〉String 
和 
String-UTF-8〉ByteArray-GBK〉String-GBK〉ByteArray-UTF-8〉String 
对应:\u0061\u4E2D\u6587(a中文)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u6D93\uE15F\u6783(a涓枃)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u4E2D\u6587(a中文) 

4、编码过程中错误诊断参考 
1)一个汉字对应一个问号 
在通过ISO-8859-1从字符串获取字节数组时,由于一个Unicode转换成一个byte,当遇到不认识的Unicode时,转换为0x3F,这样无论用哪种编码构造时都会产生一个?乱码。 
2)一个汉字对应两个问号 
在通过GBK从字符串获取字节数组时,由于一个Unicode转换成两个byte,如果此时用ISO-8859-1或用UTF-8构造字符串就会出现两个问号。 
若是通过ISO-8859-1构造可以再通过上面所说的错上加错恢复(即再通过从ISO-8859-1解析,用GBK构造); 
若是通过UTF-8构造则会产生Unicode字符"\uFFFD",不能恢复,若再通过String-UTF-8〉ByteArray-GBK〉String,则会出现杂码,如a锟斤拷锟斤拷 
3)一个汉字对应三个问号 
在通过UTF-8从字符串获取字节数组时,由于一个Unicode转换成三个byte,如果此时用ISO-8859-1构造字符串就会出现三个问号;用GBK构造字符串就会出现杂码,如a涓枃。 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qinysong/archive/2006/09/05/1179489.aspx 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值