UnsupportedOperationException with converting byte[] to float[]

前端 未结 3 1995
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 01:57

I\'m trying to convert a byte[] to a float[] by putting the byte[] in a ByteBuffer, converting this to a FloatBuffer (.asFloatBuffer), and then converting this to a

3条回答
  •  野的像风
    2021-02-15 02:36

        private static float[] toFloatArray(byte[] bytes) {
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
            FloatBuffer fb = buffer.asFloatBuffer();
    
            float[] floatArray = new float[fb.limit()];
            fb.get(floatArray);
    
    
            return floatArray;
        }
    

    ex:

         byte[] bytes = {65,-56,0,0 , 65,-56,0,0};
         float[] result = toFloatArray(bytes);   
    
         //print 25.0 25.0
         System.out.println(Arrays.toString(result));
    

提交回复
热议问题