【发布时间】:2011-01-22 00:19:03
【问题描述】:
我正在使用rs.getBytes("id") 从 JDBC ResultSet 读取一个 16 字节数组 (byte[16]),现在我需要将它转换为两个长值。我该怎么做?
这是我尝试过的代码,但我可能没有正确使用ByteBuffer。
byte[] bytes = rs.getBytes("id");
System.out.println("bytes: "+bytes.length); // prints "bytes: 16"
ByteBuffer buffer = ByteBuffer.allocate(16);
buffer = buffer.put(bytes);
// throws an java.nio.BufferUnderflowException
long leastSignificant = buffer.getLong();
long mostSignificant = buffer.getLong();
我使用以下方法将字节数组存储到数据库中:
byte[] bytes = ByteBuffer.allocate(16)
.putLong(leastSignificant)
.putLong(mostSignificant).array();
【问题讨论】:
标签: java arrays type-conversion bytebuffer bufferunderflowexception