我正在使用 JavaScriptUUID
并且需要将 UUID 转换为字节数组。奇怪的是 UUID 类不提供"toBytes()"
method.
我已经了解了这两种方法:
UUID.getMostSignificantBits()
and
UUID.getLeasSignificantBits()
但是如何将其放入字节数组中呢?结果应该是一个包含这两个值的 byte[] 。我不知何故需要进行位移,但是怎么做呢?
update:
I found:
ByteBuffer byteBuffer = MappedByteBuffer.allocate(2);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
这种做法正确吗?
还有其他方法吗(用于学习目的)?
非常感谢!!
延斯
您可以使用字节缓冲区
byte[] bytes = new byte[16];
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN);
bb.putLong(UUID.getMostSignificantBits());
bb.putLong(UUID.getLeastSignificantBits());
// to reverse
bb.flip();
UUID uuid = new UUID(bb.getLong(), bb.getLong());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)