首页 > 技术文章 > 字符编码转换

skyblogs 2014-03-31 07:30 原文

public class EncodingUtil {
    
    /**
     * 字符编码任意转换
     * @param str 字符串
     * @param encoding 原字符编码
     * @param otherEncoding 目标字符编码
     * @return 编码转换后的字符串
     */
    public static String processGetEncoding(String str,String encoding,String otherEncoding)
    {
            try
            {
                byte[] temp = str.getBytes(encoding); //按照指定编码,将字符拆串拆成字节数组
                return new String(temp,otherEncoding); //把字节数组构造成 指定编码的新字符串
                
            } catch (UnsupportedEncodingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }

    }

    /**
     * 将字符编码从"iso-8859-1"转换成"utf-8"
     * @param str 字符串
     * @return 编码转换后的字符串
     */
    public static String processGetEncoding(String str)
    {
        try
        {
            return new String(str.getBytes("iso-8859-1"),"utf-8");
            
        } catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

}

 

 

推荐阅读