I've found a phenomenon I can't really understand in Java and I post a question
- Put some data into the byte array
- Convert byte array to string
- Convert the converted string back to a byte array
- When comparing the first data and the data after conversion, some of them will be output differently.
The source code and log are below
Source code
// 1. Input byte array data
byte[] beforeBytes = new byte[]{(byte)-83, (byte)-95, (byte)-55, (byte)-49, (byte)3};
log.info("Before ByteTest");
log.info("bytesLength : " + beforeBytes.length);
for (int i = 0; i < beforeBytes.length; ++i)
{
log.info(i + " : " + (int)beforeBytes[i]);
}
// 2. Convert byte array to string
String testString = new String(beforeBytes);
// 3. Convert string to byte array
byte[] afterBytes = testString.getBytes();
log.info("After ByteTest");
log.info("bytesLength : " + afterBytes.length);
for (int i = 0; i < afterBytes.length; ++i)
{
log.info(i + " : " + (int)afterBytes[i]);
}
Log
Before ByteTest
bytesLength : 5
0 : -83
1 : -95
2 : -55
3 : -49
4 : 3
After ByteTest
bytesLength : 5
0 : 63
1 : -95
2 : -55
3 : 63
4 : 3
I want to keep the same data as the existing data even after conversion Is there a workaround?