使用Base64编码的字符串是存储通用唯一识别码(UUID)的广泛采用方法。与标准的UUID字符串表示相比,这提供了更紧凑的结果。在本文中,我们将探讨将UUID编码为Base64字符串的不同方法。
2. 使用_byte[]_和_Base64.Encoder_进行编码
我们将从使用_byte[]_和_Base64.Encoder_的最直接方法开始编码。
2.1. 编码
我们将从我们的UUID位创建一个字节数组。为此,我们将取UUID的最高有效位和最低有效位,并将它们分别放在数组的0-7和8-15位置:
byte[] convertToByteArray(UUID uuid) {
byte[] result = new byte[16];
long mostSignificantBits = uuid.getMostSignificantBits();
fillByteArray(0, 8, result, mostSignificantBits);
long leastSignificantBits = uuid.getLeastSignificantBits();
fillByteArray(8, 16, result, leastSignificantBits);
return result;
}
大约 4 分钟