UnsupportedOperationException异常与转换的byte []浮动[] [英] UnsupportedOperationException with converting byte[] to float[]
问题描述
我试图通过把字节[]中的ByteBuffer,转换给FloatBuffer向一个float []一个byte []转换( .asFloatBuffer
) ,然后转换成这一个数组。
私有静态浮动[] toFloatArray(字节[]字节){
ByteBuffer的缓冲= ByteBuffer.wrap(字节);
返回buffer.asFloatBuffer()数组();
}
但在运行:
字节[]字节= {14,32,26,21};
toFloatArray(字节);
给我一个 java.lang.UnsupportedOperationException
。
在java.nio.FloatBuffer.array(来源不明)
我相信文档说错误事做与缓冲区没有被一个数组(???)的支持。
任何人有一个想法如何解决这个问题,或者我应该如何转换这个数组浮?
私有静态浮动[] toFloatArray(字节[]字节){
ByteBuffer的缓冲= ByteBuffer.wrap(字节);
FloatBuffer FB = buffer.asFloatBuffer(); 浮动[] = floatArray新的浮动[fb.limit()];
fb.get(floatArray);
返回floatArray;
}
例如:
字节[]字节= {65,-56,0,0,65,-56,0,0};
浮动[]结果= toFloatArray(字节); //输出25.0 25.0
的System.out.println(Arrays.toString(结果));
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 an array.
private static float[] toFloatArray(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return buffer.asFloatBuffer().array();
}
However running:
byte[] bytes = {14,32,26,21};
toFloatArray(bytes);
Gives me a java.lang.UnsupportedOperationException
at java.nio.FloatBuffer.array(Unknown Source)
.
I believe the documentation says that the error has something to do with the buffer not being backed by an array (???).
Anyone has an idea how to fix this, or how I SHOULD convert this array to floats?
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));
这篇关于UnsupportedOperationException异常与转换的byte []浮动[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!