Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a UUID to base64?

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.

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.

like image 346
xenoterracide Avatar asked Jan 24 '15 20:01

xenoterracide


1 Answers

First, convert your UUID to a byte buffer for consumption by a Base64 encoder:

ByteBuffer uuidBytes = ByteBuffer.wrap(new byte[16]);
uuidBytes.putLong(uuid.getMostSignificantBits());
uuidBytes.putLong(uuid.getLeastSignificantBits());

Then encode that using the encoder:

byte[] encoded = encoder.encode(uuidBytes);

Alternatively, you can get a Base64-encoded string like this:

String encoded = encoder.encodeToString(uuidBytes);
like image 179
Alexis King Avatar answered Sep 19 '22 10:09

Alexis King