Reputation: 406
this is the encoded string
YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw
i want to generate UUID for this
Upvotes: 2
Views: 6020
Reputation: 119
The above base64 string decodes to ASCII string "b4fa2a0a-b24d-4f58-86d9-53b7b6838b67#55b1c4e3e4b0d89510c6aa27", so:
import org.apache.commons.codec.binary.Base64;
public class Solution {
private static String uuidFromBase64(String str) {
Base64 base64 = new Base64();
byte[] bytes = base64.decodeBase64(str);
String s = new String(bytes);
String trimmed = s.split("#")[0];
return trimmed;
}
}
Upvotes: 0
Reputation: 29168
You can convert as below using 2 functions. apache commons codec jar
has some methods to encode and decode UUID
using Base64
.
Link to download apache commons codec jar: http://www.java2s.com/Code/JarDownload/apache-commons/apache-commons-codec-1.4.jar.zip
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.commons.codec.binary.Base64;
public class Solution1 {
public static void main(String[] args) {
String uuid_str = "YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw";
String uuid_as_64 = uuidFromBase64(uuid_str);
System.out.println("as base64: "+uuid_as_64);
System.out.println("as uuid: "+uuidFromBase64(uuid_as_64));
}
private static String uuidToBase64(String str) {
Base64 base64 = new Base64();
UUID uuid = UUID.fromString(str);
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return base64.encodeBase64URLSafeString(bb.array());
}
private static String uuidFromBase64(String str) {
Base64 base64 = new Base64();
byte[] bytes = base64.decodeBase64(str);
ByteBuffer bb = ByteBuffer.wrap(bytes);
UUID uuid = new UUID(bb.getLong(), bb.getLong());
return uuid.toString();
}
}
Output:
as base64: 62346661-3261-3061-2d62-3234642d3466
as uuid: eb6df8eb-aeb5-fb7d-bad7-edf4eb5fb677
For more, you can follow the tutorial:
Upvotes: 4