如何在 java 中将 Base64 编码的字符串转换为 UUID
How to convert Base64 encoded string to UUID in java
这是编码后的字符串
YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw
我想为此生成 UUID
您可以使用 2 个函数进行如下转换。 apache commons codec jar
有一些使用 Base64
.
编码和解码 UUID
的方法
Link 下载 apache commons 编解码器 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();
}
}
输出:
as base64: 62346661-3261-3061-2d62-3234642d3466
as uuid: eb6df8eb-aeb5-fb7d-bad7-edf4eb5fb677
更多可以看教程:
- http://www.baeldung.com/java-base64-encode-and-decode
- http://www.tutorialspoint.com/java8/java8_base64.htm
- Storing UUID as base64 String
上面的base64字符串解码为ASCII字符串"b4fa2a0a-b24d-4f58-86d9-53b7b6838b67#55b1c4e3e4b0d89510c6aa27",所以:
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;
}
}
这是编码后的字符串
YjRmYTJhMGEtYjI0ZC00ZjU4LTg2ZDktNTNiN2I2ODM4YjY3IzU1YjFjNGUzZTRiMGQ4OTUxMGM2YWEyNw
我想为此生成 UUID
您可以使用 2 个函数进行如下转换。 apache commons codec jar
有一些使用 Base64
.
UUID
的方法
Link 下载 apache commons 编解码器 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();
}
}
输出:
as base64: 62346661-3261-3061-2d62-3234642d3466
as uuid: eb6df8eb-aeb5-fb7d-bad7-edf4eb5fb677
更多可以看教程:
- http://www.baeldung.com/java-base64-encode-and-decode
- http://www.tutorialspoint.com/java8/java8_base64.htm
- Storing UUID as base64 String
上面的base64字符串解码为ASCII字符串"b4fa2a0a-b24d-4f58-86d9-53b7b6838b67#55b1c4e3e4b0d89510c6aa27",所以:
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;
}
}