I am using DatatypeConverter to convert my strings to byte arrays and vice-versa, however when going from a byte array back to a string it doesnt report the same value as initially given.
This is a minimal example that runs on ideone
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import javax.xml.bind.DatatypeConverter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
byte[] b = new byte[20];
new Random().nextBytes(b);
String s = DatatypeConverter.printBase64Binary(b);
byte[] newB = DatatypeConverter.parseBase64Binary(s);
if(!Arrays.equals(b, newB))
System.out.println(b + " should match " + newB);
s = "Hello world";
byte[] bytes = DatatypeConverter.parseBase64Binary(s);
String newS = DatatypeConverter.printBase64Binary(bytes);
byte[] newBytes = DatatypeConverter.parseBase64Binary(newS);
if(!s.equals(newS))
System.out.println(s + " should match " + newS);
if(!Arrays.equals(bytes, newBytes))
System.out.println(bytes + " should match " + newBytes);
}
}
Which I expect to not print anything, both if statements should negate the positive match and thus not print yet It outputs:
Hello world should match Hellowor
I am having the same issue running this on my machine as part of unit tests in java 8
The weird thing is when I convert the non-matching strings back into bytes, these do match