Java – convert iso8859 string to utf8? Ä Ö Ü = > Ã why?
What's wrong with this code? I made an iso8859 string Therefore, most of them are krypooutput No problem. But how do you convert them back to normal characters (utf8 or something)?
String s = new String("Üü?öäABC".getBytes(),"ISO-8859-15"); System.out.println(s); //ÃÃŒ?öÀABC => ok(?) System.out.println(new String(s.getBytes(),"ISO-8859-15")); //ÃÂÃÅ?öÃâ¬ABC => ok(?) System.out.println(new String(s.getBytes(),"UTF-8")); //ÃÃŒ?öÀABC => huh?
Solution
A construct, such as new string ("Üü öä ABC". Getbytes(), "iso-8859-15"); It's almost always a mistake
What you do here is to get a string object, get the corresponding byte [] in the platform default code and reinterpret it as iso-8859-15 to convert it back to string
If the platform default code happens to be iso-8859-15 (or close enough to this specific string without difference, such as iso-8859-1), then it is a no operation (i.e. it has no actual effect)
In all other cases, it is likely to break string
If you try to "fix" a string, you may be too late: if you have to use a specific encoding to read data, you should use it where binary data is converted to string data For example, if you read from InputStream, you need to pass the correct encoding to the constructor of inputstreamreader
Trying to solve the problem "afterwards"
>It's harder to do > usually not even possible (because using the wrong encoding and decoding byte [] can be a destructive operation)