string decode utf-8

前端 未结 3 1962
予麋鹿
予麋鹿 2020-12-05 14:32

How can I decode an utf-8 string with android? I tried with this commands but output is the same of input:

URLDecoder.decode(\"hello&//à\", \"UTF-8\");

         


        
3条回答
  •  不思量自难忘°
    2020-12-05 15:29

    the core functions are getBytes(String charset) and new String(byte[] data). you can use these functions to do UTF-8 decoding.

    UTF-8 decoding actually is a string to string conversion, the intermediate buffer is a byte array. since the target is an UTF-8 string, so the only parameter for new String() is the byte array, which calling is equal to new String(bytes, "UTF-8")

    Then the key is the parameter for input encoded string to get internal byte array, which you should know beforehand. If you don't, guess the most possible one, "ISO-8859-1" is a good guess for English user.

    The decoding sentence should be

    String decoded = new String(encoded.getBytes("ISO-8859-1"));
    

提交回复
热议问题