1

I am taking radio stream and trying to read its metadata. It contains some Danish character which are showing as ? marks, so I tried below conversion

String s = new String(streammetadatastring.getBytes(StandardCharsets.UTF_16), StandardCharsets.ISO_8859_1);

now it removed the ? marks but showing wrong characters. I tried other ways but not getting the right values.

1
  • i also tried with different charsets, but problem persists Mar 31, 2015 at 17:54

1 Answer 1

2

A line of code like what you posted:

String s = new String(streammetadatastring.getBytes(StandardCharsets.UTF_16),
                          StandardCharsets.ISO_8859_1);

does not do anything useful.

A character encoding is a mapping from characters to bytes and vice versa. What you are doing in that line of code is convert a string to bytes using the UTF-16 character encoding, and then you immediately convert the bytes back to a string using the ISO-8859-1 encoding. That will not produce anything useful - the bytes contain UTF-16 encoded characters, not ISO-8859-1 encoded characters.

To make the Danish characters display correctly, you must make sure that wherever you display it (the command prompt window? a Linux shell? an HTML page? a GUI?) uses a font that contains the characters, and that you specify the correct character encoding (how you do that, depends on where you display the characters).

6
  • thanks for information, i am showing that it on a android textview, that textview can show danish charracter. i tried with direct put string there, and this conversion making ? marks to charracter just not the right charracter Mar 31, 2015 at 19:05
  • 1
    In what form are you receiving the data - as bytes from the radio stream? You'll have to find out what character encoding is used in the radio stream. Then convert the bytes to a string using that encoding: byte[] inputBytes = ...; String text = new String(inputBytes, <charset>);
    – Jesper
    Mar 31, 2015 at 19:52
  • thanks, u were just right, i find the function reading the data has issues it reading as Html.fromHtml(htmlresponse).toString().replace('\n', (char) 32) .replace((char) 160, (char) 32).replace((char) 65532, (char) } else { return ""; } } any suggestion what to change plz? Mar 31, 2015 at 19:57
  • It's hard to tell how exactly you should change it without knowing exactly where the data comes from and how it is handled.
    – Jesper
    Mar 31, 2015 at 20:00
  • 1
    Then it is the library's responsibility to either give you a proper UTF-16 encoded String that was internally decoded from the original HTML bytes using the correct charset, or else give you the raw HTML bytes that you can then decode to a String yourself using the HTML's charset. Mar 31, 2015 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.