Java – string decoding UTF-8
How do I decode a UTF-8 string on Android? I tried to use this command, but output the same input:
URLDecoder.decode("hello&//à","UTF-8"); new String("hello&//à","UTF-8"); EntityUtils.toString("hello&//à","utf-8");
Solution
Strings do not require encoding This is just a sequence of Unicode characters
When you want to convert a string into a sequence of bytes, you need to encode it The character set you select (UTF-8, cp1255, etc.) determines the character > byte mapping Note that characters are not necessarily translated into a single byte In most character sets, most Unicode characters are converted to at least two bytes
The encoding of strings is performed in the following ways:
String s1 = "some text"; byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into
When you have a byte sequence, you need to decode it and convert it into a string When you need to specify the character set that the string was originally encoded again (otherwise you will eventually use garbl) е dt е xt).
decode:
String s2 = new String(bytes,"UTF-8"); // Charset with which bytes were encoded
If you want to better understand this, a great text is "the absolute minimum every software developer absolutely, actively must know about Unicode and character sets (no excesses!)."