如何在Java

时间:2017-02-01 22:07:54

标签: java arrays byte

我正在尝试将double []数组转换为Base64字符串,反之亦然。 我想这样做是为了在一个简单的String中存储一个很长的双数组(double [] []),然后返回一个double [] []数组。 有可能吗?

这是我当前用于测试字节数组和Base64字符串之间转换的代码。当然,不起作用:

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;
}

}

输出:

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


3.541909365425625E83
3.052647263964711E103
1.1233976318184827E79
2261634.5098039196

1 个答案:

答案 0 :(得分:3)

你不应该在循环中使用ByteBuffer.wrap()encodeLocation()不执行任何Base64逻辑,decodeLocation()是编码,不是解码,你不需要做字符串转换自己。

简而言之,您的代码存在很多问题。以下是修复了所有问题的代码:

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;
    }
}

输出

QGVR64UeuFJARePXCj1wpECFEAAAAAAA

170.56
43.78
674.0