byte[] b = {(byte)0xe6,(byte)0x9d,(byte)0x8e};
System.out.println(new String(b));//李"李"的十六进制是 e69d8e有什么方法可以把e69d8e转换成"李"吗thanks by phoenix

解决方案 »

  1.   


    byte[] b = {(byte)0xe6,(byte)0x9d,(byte)0x8e};
    System.out.println(new String(b,"UTF-8"));//李
      

  2.   

    恩?
    先转换成byte数组,然后newString(b,"utf-8");
      

  3.   

    这个不关什么16进制的事,e69d8e 是 3 个字节。就应该猜测他是由“李”通过 UTF-8 得到的字节。然后你在逆回去就得了~
      

  4.   


                    String t = "e69d8e";
    StringBuffer sb = new StringBuffer();
    for(int i=0;i<t.length();i=i+2){
    sb.append("%").append(t.subSequence(i, i+2));
    }
    System.out.println(URLDecoder.decode(sb.toString(),"UTF-8"));
    谢谢大家好意,如果有更好的代码,我会加分.
      

  5.   


    public static String decode(String s, String s1)
            throws UnsupportedEncodingException
        {
            boolean flag = false;
            int i = s.length();
            StringBuffer stringbuffer = new StringBuffer(i <= 500 ? i : i / 2);
            int j = 0;
            if(s1.length() == 0)
                throw new UnsupportedEncodingException("URLDecoder: empty string enc parameter");
            byte abyte0[] = null;
            do
            {
                if(j >= i)
                    break;
                char c = s.charAt(j);
                switch(c)
                {
                case 43: // '+'
                    stringbuffer.append(' ');
                    j++;
                    flag = true;
                    break;            case 37: // '%'
                    try
                    {
                        if(abyte0 == null)
                            abyte0 = new byte[(i - j) / 3];
                        int k = 0;
                        do
                        {
                            if(j + 2 >= i || c != '%')
                                break;
                            abyte0[k++] = (byte)Integer.parseInt(s.substring(j + 1, j + 3), 16);
                            if((j += 3) < i)
                                c = s.charAt(j);
                        } while(true);
                        if(j < i && c == '%')
                            throw new IllegalArgumentException("URLDecoder: Incomplete trailing escape (%) pattern");
                        stringbuffer.append(new String(abyte0, 0, k, s1));
                    }
                    catch(NumberFormatException numberformatexception)
                    {
                        throw new IllegalArgumentException((new StringBuilder()).append("URLDecoder: Illegal hex characters in escape (%) pattern - ").append(numberformatexception.getMessage()).toString());
                    }
                    flag = true;
                    break;            default:
                    stringbuffer.append(c);
                    j++;
                    break;
                }
            } while(true);
            return flag ? stringbuffer.toString() : s;
        }
    java自带的解码方法