This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
both the above methods return the following, "Gratulerer! Du har n ÃƒÂ¥"
If i replaced ISO-8859-1 with UTF8, I get "Gratulerer! Du har n Ã¥"
I run the program with a -Dfile.encoding=UTF8, jvm option so as to emulate the default encoding of glassfish. if the same option were set to use ISO8859_1, i get the expected behavior, but since UTF8 is a superset of ISO8859_1, I was hoping to get the same result. I am not permitted to change the encoding of glassfish, since certain other things on the UI get falsely displayed.
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
Strings in Java are encoded as UTF-16. Always always always UTF-16. You cannot convert a String to a different encoding since they are always encoded as UTF-16.
return new String(text.getBytes(),"ISO-8859-1")
says take the String referenced by text and convert to bytes using your default encoding. Then, assume that those bytes are ISO-8859-1 and convet back to a String . If your default encoding is ISO-8859-1 and there are no characters in your string that cannot be represented in ISO-8859-1 then your new string will be exactly the same as the original - i.e. you have a null operation. If your default character encoding is not ISO-8859-1 then you will possibly (in your case certainly) corrupt the string.
You second approach has a similar problem.
If you just want the bytes of the utf-8 encoding then just use
byte utfBytesOfMyString = "my string".getBytes("utf-8");
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.