我正在使用Java的UUID
,需要将UUID转换为字节数组。奇怪的是,UUID类没有提供"toBytes()"
方法。
我已经找到了两种方法:
UUID.getMostSignificantBits()
and
UUID.getLeasSignificantBits()
但是如何将其转换为字节数组?结果应该是一个包含这两个值的byte[]数组。我需要进行位移操作,但是具体怎么实现呢?
更新:
我找到了以下代码:
ByteBuffer byteBuffer = MappedByteBuffer.allocate(2);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
这种方法正确吗?
还有其他的方法(供学习使用)吗?
非常感谢! Jens