我怎么能转换UUID为Base64? [英] How can I convert a UUID to base64?
问题描述
我要好好键入 UUID
并输出在的Base64
连接codeD格式,不过鉴于在的Base64
和输出输入法上的 UUID
如何做到这一点似乎并不明显。
I want to take type UUID
and output it in a Base64
encoded format, however given the input methods on Base64
and outputs on UUID
how to accomplish this doesn't seem obvious.
更新虽然不是我的用例明确要求,它会很高兴知道,如果使用的方法使用原始UUID的UUID的(128位,一个UUID实际上是),如标准十六进制编码一样。
update though not an explicit requirement for my use case, it would be nice to know if the method used uses the raw UUID (the 128 bits that a UUID actually is) of the UUID, as the standard hex encoding does.
推荐答案
首先,你的UUID转换为字节的缓冲区消费用的 的Base64
连接codeR :
First, convert your UUID to a byte buffer for consumption by a Base64
encoder:
ByteBuffer uuidBytes = ByteBuffer.wrap(new bytes[16]);
uuidBytes.putLong(uuid.getMostSignificantBits());
uuidBytes.putLong(uuid.getLeastSignificantBits());
然后连接code,它使用en codeR:
Then encode that using the encoder:
byte[] encoded = encoder.encode(uuidBytes);
另外,你可以得到一个Base64-CN codeD字符串是这样的:
Alternatively, you can get a Base64-encoded string like this:
String encoded = encoder.encodeToString(uuidBytes);
这篇关于我怎么能转换UUID为Base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!