【问题标题】:How to convert byte array to double array? [closed]如何将字节数组转换为双数组? [关闭]
【发布时间】:2013-08-23 19:03:48
【问题描述】:

如何将此字节数组转换为双精度数组。

byte[] byteArray = {1,0,0,1,0};

我喜欢将它从不使用循环转换,因为这个字节数组有大量元素。

请帮帮我

【问题讨论】:

  • 为什么不使用循环?
  • 每个byte 有一个double?只是复制价值?对我来说,这听起来像是一个循环的好人选……
  • @Kushan:所以?只要您不需要在调试器中手动单步执行该循环,这不是问题。计算机擅长长循环;-)
  • 这肯定需要一个循环,因为每个字节(二进制补码表示)必须物理转换为双精度(浮点表示)
  • 我仍在等待一个令人满意的答案,说明为什么“不允许循环”。

标签: java


【解决方案1】:

如果要避免循环,可以将迭代实现为递归:

public static double[] convert(byte[] in, int idx) {
    double[] ret;
    if (idx == 0) {
        ret = new double[in.length];
        ret[0] = (double)in[0];
    }
    else {
        ret = convert(in, idx-1);
        ret[idx] = (double)in[idx];
    }
    return ret;
}

public static void main(String[] args) {
    byte[] byteArray = {1,0,0,1,0};        
    double[] converted = convert(byteArray, byteArray.length-1);
    for (int i=0;i<byteArray.length;i++) {
        System.out.println(Byte.toString(byteArray[i])+ " converted to double "+converted[i]);
    }
}

哪个输出:

1 converted to double 1.0
0 converted to double 0.0
0 converted to double 0.0
1 converted to double 1.0
0 converted to double 0.0

但是:

  • 从你的问题中不清楚你想要实现什么,但是当从字节转换为双倍时,必须注意那个字节数组endianess
  • 我不建议用递归迭代替换循环,因为它的开销要大得多
  • 如果要将字节数组转换为无法完成的双数组(请参阅 kol 的回答)

【讨论】:

  • 据我所知,递归是一个循环
  • 这取决于definition
【解决方案2】:

可能的“无循环”解决方案将使用System.arraycopyArrays.copyOf,但在这些解决方案中,您无法避免将byte[] 转换为double[]。问题是根据specification,这种铸造是不可能的:

Given a compile-time reference type S (source) and a compile-time reference 
type T (target), a casting conversion exists from S to T if no compile-time 
errors occur due to the following rules.
[...]
If S is an array type SC[], that is, an array of components of type SC: 
[...] 
  - If T is an array type TC[], that is, an array of components of type TC, 
    then a compile-time error occurs unless one of the following is true: 
    - TC and SC are the same primitive type. 
    - [...]

使用循环。另见here

【讨论】:

    【解决方案3】:

    好吧,有些操作java不提供单行操作,因为它实际上并不需要。

    让我们考虑以下。我正在将您的字节数组转换为单行字符串,没有循环。

        byte[] byteArray = {1,0,0,1,0};          
        String str=Arrays.toString(byteArray);
    

    但是,

    toString() 里面发生了很多事情。

        public static String toString(byte[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";
    
        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }
    

    现在你可以看到,这里实际上有一个loop。同样,如果您能够找到将byte[] 转换为double[] 的单行方式,那么您正在做同样的事情。(带有循环)。

    你可以这样做

     byte[] byteArray = {1,0,0,1,0};
     double[] arr=toDoubleArray(byteArray);
    

    但是,你也需要以下方法。

    public static double[] toDoubleArray(byte[] byteArr){
        double[] arr=new double[byteArr.length];
        for (int i=0;i<arr.length;i++){
            arr[i]=byteArr[i];
        }
        return arr;
    }
    

    【讨论】:

      【解决方案4】:

      我假设您正在寻找 Java 答案...

      这将在一行中执行您想要的操作,但它仍会在后台循环。据我所知,如果没有在某处发生某种程度的循环,就无法投射数组

      ByteBuffer.wrap(bytes).asDoubleBuffer().array();
      

      【讨论】:

      • 我想要一个双数组而不是双值
      • 更改了代码示例,仍然存在“技术上”在某处执行循环的问题
      • 还是不行Exception in thread "main" java.lang.UnsupportedOperationException
      • @scourge192 好吧,如果我们要对它进行技术处理,那么没有循环就没有办法做到这一点,除非你手动想要执行 out[0]=(double)in[0] ;出[1]=(双)入[1];出[2]=(双)入[2];适用于所有 700,000 个元素。
      • 缓冲区需要由可访问的数组支持。你可以使用 new double[buffer.remaining()] 来达到类似的效果。
      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多