Possible Duplicate:
Conversion of byte[] into a String and then back to a byte[]
I have the following piece of code, I'm trying to get the test to pass, but can't seem to get my head around the various forms of encoding that go on in the java world.
import java.util.Arrays;
class Test {
static final byte[] A = { (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88, (byte)0x99, (byte)0x00, (byte)0xAA };
public static void main(String[] args) {
String s = new String(A);
byte[] b = s.getBytes();
if (Arrays.equals(A,b)) {
System.out.println("TEST PASSED!");
}
else {
System.out.println("TEST FAILED!");
}
}
}
I guess my question is: What is the correct way to convert a byte array of arbitary bytes to a Java String, then later on convert that same Java String to another byte array, which will have the same length and same contents as the original byte array?
for(byte iB : b) System.out.println(Integer.toHexString(iB ));
, you'll see the trouble starts at 0x88. – Bala R Sep 22 '11 at 1:24