String.getBytes() Giving different values in Windows and Linux
802203Oct 30 2010 — edited Nov 2 2010Hello 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 ?