中文解码Unicode

Posted wangchuanfu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中文解码Unicode相关的知识,希望对你有一定的参考价值。

package com.j1.search.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.lang.StringUtils;

public class WebUtils
{
    public static String convertUnicode(String unicodeStr) throws UnsupportedEncodingException
    {
        if(StringUtils.isNotBlank(unicodeStr) && unicodeStr.startsWith("%"))
        {
            while(unicodeStr.contains("%25"))
            {
                unicodeStr = unicodeStr.replace("%25", "%");
            }
            
            if(unicodeStr.length() == 3)
            {
                unicodeStr = URLDecoder.decode(unicodeStr, "utf-8");
            }
            else
            {
                boolean flag = true;
                for(int i = -1; i < 10 && (i + 3) < unicodeStr.length(); i += 3)
                {
                    if(unicodeStr.indexOf("%", i) != (i + 1))
                        flag = false;
                }
                if(flag)
                {
                    unicodeStr = URLDecoder.decode(unicodeStr, "utf-8");
                }
            }
            return unicodeStr;
        }
        else
        {
            return unicodeStr;
        }
    }
    
    public static String convertGetParams(String isoStr) throws UnsupportedEncodingException
    {
        if(StringUtils.isNotBlank(isoStr))
        {
            return new String(isoStr.getBytes("iso8859-1"),"UTF-8");
        }
        else
        {
            return isoStr;
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        String aa = "%E6%AC%A7%E5%A7%86%E9%BE%99%E8%A1%80%E5%8E%8B%E8%AE%A1";// 给个默认值
        System.out.println(convertUnicode(aa));
    }
    
}

 

以上是关于中文解码Unicode的主要内容,如果未能解决你的问题,请参考以下文章

unicode,bytes,中文(utf-8/gbk) 编解码 | python

js中文转Unicode编码与解码

hive udf unicode解码

hive udf unicode解码

中文解码Unicode

pyopencl中基本字符串复制程序中的Unicode解码错误