-1

I'm confused about this issue. Platform is on win7 java8.

Sample code:

String encryptedData = "0019ZfGO0nefTb2kIuHO0M3hGO09ZfGF";
Base64.Decoder decoder = Base64.getDecoder();
byte[] dataByte = decoder.decode(encryptedData);
System.out.println(dataByte);
dataByte = decoder.decode(encryptedData);
System.out.println(dataByte);

The output:

[B@15db9742
[B@6d06d69c

The exact input got different result. Don't know if there's anyway to clear the status and make the result consistent every time?

Thanks!

cynkiller
  • 73
  • 9
  • 1
    `decoder.decode(encryptedData);` returns a new `byte[]`. What you're printing is the reference of that array, which is different each time decode is called. The data they both contain will be the same though. – Aaron May 28 '18 at 16:30
  • See [How to convert a byte array to a hex string in Java?](https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java) – vanje May 28 '18 at 16:32
  • I understand the problem now. Thank you! – cynkiller May 28 '18 at 16:38

1 Answers1

1

In Java, arrays don't override toString(), so if you try to print one directly, you get the "className + @ + the hex of the hashCode of the array", as defined by Object.toString()
Note:Just printing the array by reference variable means you are calling the toString() method of that array object.

As decoder.decode(encryptedData) returns a new byte[] every-time, therefore it gives a different value when you just print the reference variable.
Ex: System.out.println(dataByte);//output:[B@15db9742

You can use the standard library functions to print the contains of the array. There are many ways to achieve this. Just some examples are below:
System.out.println(Arrays.toString(dataByte));
System.out.println(dataByte.toList());

madup
  • 66
  • 3