android - Converting byte[] to float[] -


i'm trying convert float[] byte[] in android. i've looked @ other questions , answers on stackoverflow none of them helped decided implement own solution. way have tried is:

byte[] data = data; bytebuffer buffer = bytebuffer.allocate(data.length); buffer.put(data); floatbuffer fbuffer = buffer.asfloatbuffer(); float[] dataconverted = fbuffer.array(); 

however, exception:

java.lang.unsupportedoperationexception     @ java.nio.bytebufferasfloatbuffer.protectedarray(bytebufferasfloatbuffer.java:128)     @ java.nio.floatbuffer.array(floatbuffer.java:101) 

from line:

    float[] dataconverted = fbuffer.array(); 

keeps being thrown, , i'm not sure why. explain why exception being thrown; of great help?

the floatbuffer allocated not have backing array return; please refer floatbuffer.hasarray(). achieve effect like, try copying floatbuffer float array via floatbuffer.get(float[])

sample code:

final byte[] data = new byte[] {         64, 73, 15, -48, 127, 127, -1, -1, 0, 0, 0, 1, 0, 0, 0, 0 };  final floatbuffer fb = bytebuffer.wrap(data).asfloatbuffer(); final float[] dst = new float[fb.capacity()]; fb.get(dst); // copy contents of floatbuffer dst  (int = 0; < dst.length; i++) {     system.out.print(dst[i]);     if (i == dst.length - 1) {         system.out.println();     } else {         system.out.print(", ");     } } 

output:

3.14159, 3.4028235e38, 1.4e-45, 0.0 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -