Skip to content
Advertisement

How to convert binary payload (file) to byte[] in java?

I receive json data that contains binary data like that ,and I would like to convert that data to byte[] in java but I don’t know how ?.

"payload": "7V1bcxs3ln6frfdcfvfbghfdX8HSw9Zu1QzzartyhblfdcvberCObjvJpkiJUpmhRI1pKXYeXHRsZLSrCy
5dElN5tfvQaO72TdSoiOS3TH8Yxdffgtg754679513qdfrgvlslsqdeqaepdccngrdzedrtghBD+d++e7v//p80/v96v7h+u72
+z1gfK/39x/+9t391cPTzeP88aE/++Fvvd53n+8+Xd1c/fBm/unqAf+7
N7v65en++vGP3vx2fvPHw/XDdwfpHf5mevhq/vQDcnAAwD+gEPwDF+bDxTv+3UF61d/4eesrfP356uFx"

Advertisement

Answer

Based on the observation that the “binary” string consists of ASCII letters, digits and “+” and “/”, I am fairly confident that it is actually Base64 encoded data.

To decode Base64 to a byte[] you can do something like this:

String s = "7V1bcxs3ln6...";
byte [] bytes = java.util.Base64.getDecoder().decode(s);

The decode call will throw IllegalArgumentException if the input string is not properly Base64 encoded.


When I decoded that particular string using an online Base64 decoder, the result is unintelligible. But that is what I would expect for an arbitrary “blob” of binary data.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement