获得未知的字符编码格式转换

/**
 * 
 */
package com.chinabase.common.util;

import java.io.UnsupportedEncodingException;



/**
 * 获得未知的字符编码格式*/
public class UnknowEncoding {
    /**
     * 获得未知编码格式
     * 
     * @param bs
     */
    public String getEncoding(String yourstring) {
//        System.out.println(yourstring);
        byte[] bs;
        String[] encodings = { CharContents.GB2312, CharContents.GBK,
                CharContents.ISO_8859_1, CharContents.US_ASCII,
                CharContents.UTF_16, CharContents.UTF_16BE,
                CharContents.UTF_16LE, CharContents.UTF_8 };
        try {
            for (int i = 0; i < encodings.length; i++) {
                bs=yourstring.getBytes(encodings[i]);
//                System.out.println((int)bs [1]);
                if (yourstring.equals(new String(bs,encodings[i]))) {
                    return encodings[i];
                }
            }
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return "unknow";
    }

    public static void main(String[] args) {
        UnknowEncoding u = new UnknowEncoding();
        ChangeCharset c=new ChangeCharset();
        try {
            System.out.println(u.getEncoding(c.toUTF_16("str")));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(u.getEncoding("[?°??????]?????????°???????ò?ú?????????°?°???±??"));
        
    }
}

 

posted on 2014-01-09 10:27  蝌蚪的精神  阅读(280)  评论(0编辑  收藏  举报

导航