I tested Marko Topolnik's suggestion, it seems to work:
public class CharsetTest
{
@Test
public void test()
{
byte[] allByteValues = new byte[256];
byte byteValue = Byte.MIN_VALUE;
for(int i = 0; i < allByteValues.length; i++)
{
allByteValues[i] = byteValue;
byteValue++;
}
{
System.out.println(Arrays.toString(allByteValues));
String string = new String(allByteValues, StandardCharsets.US_ASCII);
System.out.println(string);
byte[] bytesFromString = string.getBytes(StandardCharsets.US_ASCII);
System.out.println(Arrays.toString(bytesFromString));
System.out.println("equal: " + Arrays.equals(allByteValues, bytesFromString));
System.out.println();
Assert.assertFalse(Arrays.equals(allByteValues, bytesFromString));
}
{
System.out.println(Arrays.toString(allByteValues));
String string = new String(allByteValues, StandardCharsets.ISO_8859_1);
System.out.println(string);
byte[] bytesFromString = string.getBytes(StandardCharsets.ISO_8859_1);
System.out.println(Arrays.toString(bytesFromString));
System.out.println("equal: " + Arrays.equals(allByteValues, bytesFromString));
System.out.println();
Assert.assertTrue(Arrays.equals(allByteValues, bytesFromString));
}
}
}
The output on my eclipse console: