I have a String with a "ñ" character and I have some problems with it. I need to encode this String to UTF-8 encoding. I have tried it by this way, but it doesn't work:
byte ptext = myString.getBytes();
String value = new String(ptext, "UTF-8");
String objects in Java don't have an encoding (*).
The only thing that has an encoding is a
byte. So if you need UTF-8 data, then you need a
byte. If you have a
String that contains unexpected data, then the problem is at some earlier place that incorrectly converted some binary data to a
String (i.e. it was using the wrong encoding).
(*) that's not entirely accurate. Actually they have an encoding, but that's UTF-16 and can't be modified.