简体   繁体   中英

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

I'm trying to convert a double[] array to Base64 string and vice versa. I want to do this for store a very long array of double arrays (double[][]) in a simple String and then return back to a double[][] array. Is it possible?

This is my current code for testing the conversion between byte array and Base64 String. Of course, doesn't work:

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
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){
    byte[] bytes = doubleToByteArray(doubleArray);
    String base64Encoded = new String(bytes, StandardCharsets.UTF_8);
    return base64Encoded;
}

private static double[] decodeLocation(String base64Encoded){
    byte[] bytes = Base64.getEncoder().encode(base64Encoded.getBytes());
    double[] doubleArray = byteToDoubleArray(bytes);
    return doubleArray;
}

private static byte[] doubleToByteArray(double[] doubleArray){

    int times = Double.SIZE / Byte.SIZE;
    byte[] bytes = new byte[doubleArray.length * times];
    for(int i=0;i<doubleArray.length;i++){
        ByteBuffer.wrap(bytes, i*times, times).putDouble(doubleArray[i]);
    }   
    return bytes;
}

private static double[] byteToDoubleArray(byte[] bytes){
    int times = Double.SIZE / Byte.SIZE;
    double[] doubles = new double[bytes.length / times];
    for(int i=0;i<doubles.length;i++){
        doubles[i] = ByteBuffer.wrap(bytes, i*times, times).getDouble();
    }
    return doubles;
}

}

Output:

@eQ??R@E??
=p?@?


3.541909365425625E83
3.052647263964711E103
1.1233976318184827E79
2261634.5098039196

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM