如何将UUID转换为base64?

xen*_*ide 4 java uuid base64 encoding

我想采用类型UUID并以Base64编码格式输出它,但是如果输入方法Base64和输出UUID如何实现这一点似乎并不明显.

更新虽然不是对我的用例的明确要求,但是很高兴知道所使用的方法是否使用UUID的原始UUID(UUID实际上是128位),正如标准的十六进制编码那样.

Ale*_*ing 7

首先,将您的UUID转换为字节缓冲区以供Base64编码器使用:

ByteBuffer uuidBytes = ByteBuffer.wrap(new bytes[16]);
uuidBytes.putLong(uuid.getMostSignificantBits());
uuidBytes.putLong(uuid.getLeastSignificantBits());
Run Code Online (Sandbox Code Playgroud)

然后使用编码器对其进行编码:

byte[] encoded = encoder.encode(uuidBytes);
Run Code Online (Sandbox Code Playgroud)

或者,您可以像这样获得Base64编码的字符串:

String encoded = encoder.encodeToString(uuidBytes);
Run Code Online (Sandbox Code Playgroud)