I found the following code in SO. Does this really work?
String xml = new String("áéíóúñ");
byte[] latin1 = xml.getBytes("UTF-8");
byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");
I mean, latin1
is UTF-8-encoded in the second line, but read als ISO-8859-1-encoded in the third? Can this ever work?
Not that I did not want to criticize the cited code, I am just confused since I ran into some legacy code that is very similar, that seems to work, and I cannot explain why.
EDIT: I guess in the original post, "UTF-8" in line 2 was just a TYPO. But I am not sure ...
EDIT2: After my initial posting, someone edited the code above and changed the 2nd line to byte[] latin1 = xml.getBytes("ISO-8859-1");
. I don't know who did that and why he did it, but clearly this messed up pretty much. Sorry to all who saw the wrong version of the code. I don't know who edited it. The code above is correct now.