I'm trying to convert a byte array to a String, then back to a byte array. The first part (byte[] to string) works, when I try to convert the string back to a byte array then compare what I get with my initial byte array, I find out they're different. I'm guessing it's an encoding issue, I tried different solutions (using UTF-8, ISO-8859-1, UTF-16LE and others) but none seem to work.
Would anyone know how to solve this problem? Thanks in advance
Path path = Paths.get("C:\\folder1", "profil1.bmp");
try {
//file to byte[]
byte[] byte_array = Files.readAllBytes(path);
System.out.println(Arrays.toString(byte_array ));
//byte[] to string
String byte_string = Arrays.toString(byte_array);
//String to byte[]
byte[] string_byte = byte_string.getBytes();
System.out.println(Arrays.equals(byte_array, string_byte));
} catch (IOException e) {
System.out.println(e);
}
Here's the output: (The result was too long, so I cut of a part of it)
[66, 77, -10, -44, 1, 0, 0, 0, 0, 0, 1, -1, ....... ,-1]
false