0

I've got a problem with this code, I'm loosing the ° character :

StringBuffer libelle = new StringBuffer("Name : ");
libelle.append(lCurrentLine[2]);

try {
    libelle.append(new String(" - N° : ".getBytes("UTF-8"), "ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
    libelle.append(" - N : ");
}

After this code I have Name : blabla - N° : blabla in my StringBuffer instead of Name : blabla - N° : blabla

Any idea ?

deHaar
  • 17,687
  • 10
  • 38
  • 51
user3469203
  • 537
  • 1
  • 9
  • 22
  • what is the java file encoding? – jhamon Jan 07 '19 at 09:23
  • you can read [this](https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding) – Mahmoud Jan 07 '19 at 09:25
  • 1
    You having utf encoded string and pass it to method that want ISO-8859-1 what else do you expect? What you want to achieve? – talex Jan 07 '19 at 09:31
  • Not sure why but `.getBytes("UTF-16BE")` works. – Nicholas K Jan 07 '19 at 09:49
  • It's unclear what you are attempting to do. Java's text datatypes use UTF-16. Both UTF-16 and UTF-8 are character encodings for the Unicode character set. UTF-8 would only come into play when transferring text in a String to/from a file or stream. – Tom Blodget Jan 08 '19 at 02:29
  • 1
    `°` is the UTF-8 encoded form of `°` when interpretted as ISO-8859-1 instead of as UTF-8. Just like you asked it to. But why are you encoding the appended string at all? Why not just append it as-is? `libelle.append(" - N° : ");` What is the reason for encoding? What are you planning on doing with the `StringBuffer` after it is built up? Why do you need it encoded in ISO-8859-1? I think you should be handling that encoding at a different layer of your code, if at all. – Remy Lebeau Jan 08 '19 at 21:09

0 Answers0