string decode utf-8

前端 未结 3 1982
予麋鹿
予麋鹿 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:18

    A string needs no encoding. It is simply a sequence of Unicode characters.

    You need to encode when you want to turn a String into a sequence of bytes. The charset the you choose (UTF-8, cp1255, etc.) determines the Character->Byte mapping. Note that a character is not necessarily translated into a single byte. In most charsets, most Unicode characters are translated to at least two bytes.

    Encoding of a String is carried out by:

    String s1 = "some text";
    byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into
    

    You need to decode when you have а sequence of bytes and you want to turn them into a String. When yоu dо that you need to specify, again, the charset with which the bytеs were originally encoded (otherwise you'll end up with garblеd tеxt).

    Decoding:

    String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 
    

    If you want to understand this better, a great text is "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"

提交回复
热议问题