我需要将一个字节数组转换为双精度。我正在使用
double dvalue = ByteBuffer.wrap(value).getDouble();但是在运行时,我得到了BufferUnderflowException异常
Exception in thread "main" java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:498)
at java.nio.HeapByteBuffer.getDouble(HeapByteBuffer.java:508)
at Myclass.main(Myclass.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)我需要在这里更改什么?
发布于 2014-09-09 12:43:33
ByteBuffer#getDouble()抛出
BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer所以value必须包含少于8个字节。double是64位、8字节的数据类型。
发布于 2014-09-09 12:43:34
您的代码如下所示:
byte [] value = { // values };
double dvalue = ByteBuffer.wrap(value).getDouble();如果是,那么它应该是有效的。
并向我们展示您的value数组数据。
在oracle docs中:
Throws: BufferUnderflowException - If there are fewer than eight bytes remaining in this buffer为了修复它,您需要确保ByteBuffer中有足够的数据以便读取双(8 bytes)。
看看Here是一段简单的代码,它显示了你想要的输入数据和输出数据。
https://stackoverflow.com/questions/25736794
复制相似问题