我正在读取一个来自JDBC ResultSet
与rs.getBytes("id")
的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();
+1啊,这更美丽。谢谢。 – Jonas 2011-02-05 07:20:23