Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

String.getBytes() Giving different values in Windows and Linux

802203Oct 30 2010 — edited Nov 2 2010
Hello friends,

I have written a sample program to create a String from a byte array. After creating the String , I called the 'getBytes()' method of the newly constructed string and run in Windows and Linux machines. But I am getting different outputs in both the OS. I am get the expected output in Windows, but in Linux the output is wrong for me.

I am giving the code I tried below:

public class Test {

public static void main(String[] args) {

byte arr[] = new byte[]{-80};

String sampleStr = new String(arr);
byte arrDec[] = sampleStr.getBytes(); // Here I am expecting the same bytes as the 'arr' from which I created this string

for(int i=0;i<arrDec.length;i++)
System.out.println(arrDec[i] );

//System.out.println(java.nio.charset.Charset.defaultCharset().name());
}

The Output from Windows : -80 ( The charset is 'windows-1252' )

The Output from Linux : -17 ( The charset is 'UTF-8' )
-65
-67

Please tell me why this is happening ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2010
Added on Oct 30 2010
2 comments
1,381 views