Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.getBytes("ISO-8859-1") gives me 16-bit characters on OS X

Using Java 6 to get 8-bit characters from a String:

System.out.println(Arrays.toString("öä".getBytes("ISO-8859-1")));

gives me, on Linux: [-10, 28] but OS X I get: [63, 63, 63, -89]

I seem get the same result when using the fancy new nio CharSetEncoder class. What am I doing wrong? Or is it Apple's fault? :)

like image 665
lennartcl Avatar asked Feb 10 '10 08:02

lennartcl


2 Answers

I managed to reproduce this problem by saving the source file as UTF-8, then telling the compiler it was really MacRoman:

javac -encoding MacRoman Test.java

I would have thought javac would default to UTF-8 on OSX, but maybe not. Or maybe you're using an IDE and it's defaulting to MacRoman. Whatever the case, you have to make it use UTF-8 instead.

like image 199
Alan Moore Avatar answered Sep 20 '22 10:09

Alan Moore


What is the encoding of the source file? 63 is the code for ? which means "character can't be converted to the specified encoding".

So my guess is that you copied the source file to the Mac and that the source file uses an encoding which the Mac java compiler doesn't expect. IIRC, OS X will expect the file to be UTF-8.

like image 33
Aaron Digulla Avatar answered Sep 19 '22 10:09

Aaron Digulla