Java中字节数组和UUID之间的转换

2023/06/09

1. 概述

在这个简短的教程中,我们将了解如何在Java中在字节数组和UUID之间进行转换

2. 将UUID转换为字节数组

我们可以在纯Java中轻松地将UUID转换为字节数组

public static byte[] convertUUIDToBytes(UUID uuid) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return bb.array();
}

3. 将字节数组转换为UUID

将字节数组转换为UUID同样简单:

public static UUID convertBytesToUUID(byte[] bytes) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    long high = byteBuffer.getLong();
    long low = byteBuffer.getLong();
    return new UUID(high, low);
}

4. 测试

让我们测试一下我们的方法:

UUID uuid = UUID.randomUUID();
System.out.println("Original UUID: " + uuid);

byte[] bytes = convertUUIDToBytes(uuid);
System.out.println("Converted byte array: " + Arrays.toString(bytes));

UUID uuidNew = convertBytesToUUID(bytes);
System.out.println("Converted UUID: " + uuidNew);

结果将如下所示:

Original UUID: bd9c7f32-8010-4cfe-97c0-82371e3276fa
Converted byte array: [-67, -100, 127, 50, -128, 16, 76, -2, -105, -64, -126, 55, 30, 50, 118, -6]
Converted UUID: bd9c7f32-8010-4cfe-97c0-82371e3276fa

5. 总结

在这个快速教程中,我们学习了如何在Java中在字节数组和UUID之间进行转换。

与往常一样,本教程的完整源代码可在GitHub上获得。

Show Disqus Comments

Post Directory

扫码关注公众号:Taketoday
发送 290992
即可立即永久解锁本站全部文章