How to Convert a Byte Array to Base64 in Java

Why does jackson convert byte array to base64 string on converting to json?

What is wrong in passing byte array as is to the json String as an array of numbers?

Nothing, if you're happy with each byte of input taking (on average, assuming even distribution of bytes) 3.57 characters. That's assuming you don't have a space after each comma - otherwise it's 4.57 characters.

So compare these data sizes with 10K of data:

  • Raw: 10240 bytes (can't be represented directly in JSON)
  • Base64: 13656 characters
  • Array of numbers: 36556 characters

The size increase of 33% for base64 is painful enough... the size increase of using an array is much, much worse. So the convention is to use base64 instead. (It's only a convention - it's not like it's baked into the JSON spec. But it's followed by most JSON encoders and decoders.)

How to convert double array to Base64 string and vice versa in Java

You should not use ByteBuffer.wrap() inside a loop, your encodeLocation() doesn't do any Base64 logic, the decodeLocation() is encoding, not decoding, and you don't need to do string conversion yourself.

In short, your code has lots of issues. Here is the code with all the problems fixed:

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.util.Base64;

public class Main {
public static void main(String[] args) {
double[] testArray = {170.56, 43.78, 674.0};

String encodedTest = encodeLocation(testArray);
System.out.println(encodedTest+"\n");

double[] decodedTest = decodeLocation(encodedTest);
for (double d : decodedTest)
System.out.println(d);
}
private static String encodeLocation(double[] doubleArray) {
return Base64.getEncoder().encodeToString(doubleToByteArray(doubleArray));
}
private static double[] decodeLocation(String base64Encoded) {
return byteToDoubleArray(Base64.getDecoder().decode(base64Encoded));
}
private static byte[] doubleToByteArray(double[] doubleArray) {
ByteBuffer buf = ByteBuffer.allocate(Double.SIZE / Byte.SIZE * doubleArray.length);
buf.asDoubleBuffer().put(doubleArray);
return buf.array();
}
private static double[] byteToDoubleArray(byte[] bytes) {
DoubleBuffer buf = ByteBuffer.wrap(bytes).asDoubleBuffer();
double[] doubleArray = new double[buf.limit()];
buf.get(doubleArray);
return doubleArray;
}
}

Output

QGVR64UeuFJARePXCj1wpECFEAAAAAAA

170.56
43.78
674.0

Convert ByteArray to Base64 in Kotlin

If you are using Kotlin with Java, you can use java.util.Base64 to encode a ByteArray into a String. I wrote an extension function to do this:

fun ByteArray.toBase64(): String = 
String(Base64.getEncoder().encode(this))

// Use:
val b64 = "asdf".toByteArray().toBase64()
// YXNkZg==

Base64 of byte array in Java and it's equivalent in PHP

PHP will already treat $string as a byte string, so you don't need to unpack/implode it.

If you do this:

$string = 'ec65450a-5:5217e';
$bytes = unpack('C*', $string);
echo implode('', $bytes);

You get this:

1019954535253489745535853504955101

Which is a mushed together list of integer base 10 ASCII values of each character, and is almost certainly not what you want. Just encode the string directly:

echo base64_encode($string);

Result:

ZWM2NTQ1MGEtNTo1MjE3ZQ==

Also, you'll want to change your password now that you've posted it here. :)



Related Topics



Leave a reply



Submit