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
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.
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;
}
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:
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();
Exception in thread "main" java.lang.UnsupportedOperationException
doubleperbyte? Just copying the value? Sounds like a good candidate for a loop to me...