java中判斷字符編碼以及轉碼

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


發佈了37 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章