We have used byte[].toStrng() method to convert an encoded string and save it in the DB. But we need to store the string in UTF-8 encoded format. Unfortunatey now we are unable to convert the string back to byte[] as the getBytes is returning different value than the original byte[] and decoding is also getting failed on the new byte array.
Is there any other way to convert the string to original byte array or decode to original string successfully?
public static void main (String[] args) {
String strToEncode = "test sample";
byte[] encbytes = Base64.getEncoder().encode(strToEncode.getBytes());
String str = encbytes.toString();
System.out.println (" Byte Array is " + encbytes);
System.out.println (" String is " + str);
byte[] newbytes = str.getBytes();
System.out.println (" New Byte Array is " + newbytes);
}
Output is as follows:
Byte Array is [B@15db9742
String is [B@15db9742
New Byte Array is [B@6d06d69c
Need encbytes and newbytes Array to be same. Any help is appreciated