2

How to convert this byte array to double array.

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

I like to convert this from not using a loop because this byte array have a bulk of elements.

Please help me

9
  • 6
    Why not use a loop? Commented Aug 21, 2013 at 12:05
  • 5
    With one double per byte? Just copying the value? Sounds like a good candidate for a loop to me... Commented Aug 21, 2013 at 12:06
  • 12
    @Kushan: so? as long as you don't need to manually step through that loop in your debugger that's not a problem. Computers are good at doing long loops ;-) Commented Aug 21, 2013 at 12:10
  • 1
    This will definitely require a loop on some level as each byte (two's complement representation) has to be physically converted to a double (floating point representation) Commented Aug 21, 2013 at 12:23
  • 2
    I for one am still waiting for a satisfactory answer as to why loops "aren't allowed". Commented Aug 21, 2013 at 12:28

4 Answers 4

4

Possible "loopless" solutions would use System.arraycopy or Arrays.copyOf, but in these solutions you cannot avoid casting your byte[] to a double[]. The problem is this casting is impossible according to the 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. 
    - [...]

Use a loop. See also here.

Sign up to request clarification or add additional context in comments.

Comments

2

Ok, there are some operation java not provide single line operations, since it is not actually needed.

Let's consider following. I am converting your byte array to String in single line with no loops.

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

But,

Inside that toString() many things going on.

    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(", ");
    }
}

Now you can see, actually there is a loop here. Similarly if you able to find a single line way to convert byte[] to double[] you are doing same thing.(with loops).

You can do this as follows

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

But, you need following method too.

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

Comments

1

If you want to avoid loops, iteration can be implemented as recursion:

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

which outputs:

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

However:

  • it is not clear from your question what are you trying to achieve but when converting from bytes to double you must be aware of that byte array endianess
  • I wouldn't recommend replacing loops with recursive iteration because it has significantly more overhead
  • if you want to cast the byte array to double array that cannot be done (see kol's answer)

2 Comments

as far as i know, recursion is a loop
that depends on the definition
-3

I'm assuming that you're looking for a Java answer...

This will do what you want in a single line, but it still does looping in the background. As far as I know, there is not a way to cast an array without some degree of looping taking place somewhere

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

7 Comments

i want a double array not double value
changed the code sample, still has the problem of "technically" doing the loop somewhere
Still not really working Exception in thread "main" java.lang.UnsupportedOperationException
@scourge192 Well, if we're going to be technical about it, there's literally no way to do this without loops unless you manually want to do out[0]=(double)in[0]; out[1]=(double)in[1]; out[2]=(double)in[2]; for all 700,000 elements.
The buffer needs to be backed by an accessible array. You could use new double[buffer.remaining()] to achieve a similar effect.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.