@字符串编码判断函数

//编码判断函数
    public static String getEncoding(String str) {      
        String encode = "GB2312";      
        try {      
            if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s = encode;      
                return s;      
            }      
        } catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
        try {      
            if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s1 = encode;      
                return s1;      
            }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
        try {      
            if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s2 = encode;      
                return s2;      
            }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
        try {      
            if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s3 = encode;      
                return s3;      
            }      
        } catch (Exception exception3) {      
        }      
        return "";      
    }    

 

@编码转换函数

1,字符串和unicode编码之间的转换函数

(1)//字符串转换为unicode编码函数
    /*
    * 将字符串转成unicode
    * @param str 待转字符串
    * @return unicode字符串
    */
    public String convert(String str){   
        str = (str == null ? "" : str);
        String tmp;
        StringBuffer sb = new StringBuffer(1000);
        char c;
        int i, j;
        sb.setLength(0);
        for (i = 0; i < str.length(); i++)
        {
            c = str.charAt(i);
            sb.append("\\u");
            j = (c >>>8); //取出高8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
            j = (c & 0xFF); //取出低8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
        }
        return (new String(sb));
    }
    
(2) //unicode转成字符串,与上述过程反向操作即可
    /*
    * 将unicode 字符串
    * @param str 待转字符串
    * @return 普通字符串
    */
    public String revert(String str)
    {
        str = (str == null ? "" : str);
        if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回
            return str;

        StringBuffer sb = new StringBuffer(1000);
        for (int i = 0; i < str.length() - 6;)
        {
            String strTemp = str.substring(i, i + 6);
            String value = strTemp.substring(2);
            int c = 0;
            for (int j = 0; j < value.length(); j++)
            {
                char tempChar = value.charAt(j);
                int t = 0;
                switch (tempChar)
                {
                case 'a':
                    t = 10;
                    break;
                case 'b':
                    t = 11;
                    break;
                case 'c':
                    t = 12;
                    break;
                case 'd':
                    t = 13;
                    break;
                case 'e':
                    t = 14;
                    break;
                case 'f':
                    t = 15;
                    break;
                default:
                    t = tempChar - 48;
                    break;
                }
                c += t * ((int) Math.pow(16, (value.length() - j - 1)));
            }
            sb.append((char) c);
            i = i + 6;
        }
        return sb.toString();
    }

2,//直接将字符串转换为指定编码的函数其实就是对new String(bs, newCharset);类的封装
    //str是要装换的字符串
    //newCharset是要转换的编码格式
    public String changeCharset(String str, String newCharset)  
            throws UnsupportedEncodingException {
        if (str != null) {
            //用默认字符编码解码字符串。
            byte[] bs = str.getBytes();
            //用新的字符编码生成字符串
            return new String(bs, newCharset);
        }else{
            return "";
        }
    }

---上边函数就是对String str=new String(name.getBytes("ISO-8859-1"),"utf-8");的封装name字符串是ISO-8859-1编码格式,这里的作用是将name字符串转换为utf8编码。

posted on 2014-04-17 14:40  学到老死  阅读(216)  评论(0)    收藏  举报