Java中判断字符编码及转码

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.     }   
  143. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值