0

I'm building SMPP gateway that gets byte[] array of indian chars and converts it to readable string that forwarded by email. In Win machine, this code is working:

byte[] data= ....;
shortMessage = new String(data, GSMCharset.forName("UTF-16"));

In Linux however, its give rubbish.

I tried other charset options, but all give me nothing. Any ideas how to make it work on Linux.

(The DataCoding == 8)

Arnold
  • 323
  • 2
  • 15

1 Answers1

1

It seems the encoding of the output is controlled by the encoding of the source file. Unless specified at compile time (How can I specify the encoding of Java source files?), the default encoding is inherited from the OS.

I am guessing the Windows machine you used had a default encoding that caused the output you are expecting, while the Linux machine did not. See this question for a similar issue reported - Charset of Java source file and failing test.

I was able to reproduce the behavior. Also found a fix - changing the encoding of the source file. Read on for details.

I ran the following code in two different encodings.

System.out.println(Charset.defaultCharset().toString());
byte[] data = new byte[] {9, 22, 9, 65, 9, 54, 9, 22, 9, 44, 9, 48, 9, 64};
System.out.println(Arrays.toString(data));
System.out.println(new String(data, "UTF-16"));

Using default encoding of OS

In my case, it was "MacRoman" on my mac. The output is this:

MacRoman
[9, 22, 9, 65, 9, 54, 9, 22, 9, 44, 9, 48, 9, 64]
???????

Using UTF-8 encoding

I changed the encoding of the source file (see the "Properties" of the source file). Ran again. The output is this:

UTF-8
[9, 22, 9, 65, 9, 54, 9, 22, 9, 44, 9, 48, 9, 64]
खुशखबरी
Community
  • 1
  • 1
Wahid Sadik
  • 908
  • 10
  • 24
  • It seems you are right, when running my code, I get US-ASCCI as the defaultCharset. However, also after I changed the text file encoding to UTF-8 in the eclipse file properties is the same - show US-ASCCI. – Arnold Jun 24 '13 at 16:28
  • Try using "-Dfile.encoding=UTF-8" command line parameter with java. Shown here - http://stackoverflow.com/questions/4885114/utf-8-encoding-problem-in-java-text-output. Also can be font issue - http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html. – Wahid Sadik Jun 24 '13 at 17:01
  • added -Dfile.encoding=UTF-8 to the command that runs the jar file, everything is working now, Thanks! – Arnold Jun 24 '13 at 17:12