我一直在尝试使用UUID作为数据库密钥.我想尽可能占用最少的字节数,同时仍然保持UUID表示可读.
我认为我已经使用base64将它降低到22个字节,并删除了一些似乎没有必要为我的目的存储的尾随"==".这种方法有什么缺陷吗?
基本上我的测试代码会进行一系列转换,以将UUID降低到22字节的字符串,然后将其转换回UUID.
import java.io.IOException;
import java.util.UUID;
public class UUIDTest {
public static void main(String[] args){
UUID uuid = UUID.randomUUID();
System.out.println("UUID String: " + uuid.toString());
System.out.println("Number of Bytes: " + uuid.toString().getBytes().length);
System.out.println();
byte[] uuidArr = asByteArray(uuid);
System.out.print("UUID Byte Array: ");
for(byte b: uuidArr){
System.out.print(b +" ");
}
System.out.println();
System.out.println("Number of Bytes: " + uuidArr.length);
System.out.println();
try {
// Convert a byte array to base64 string
String s = new sun.misc.BASE64Encoder().encode(uuidArr);
System.out.println("UUID Base64 String: " +s);
System.out.println("Number of Bytes: " + …Run Code Online (Sandbox Code Playgroud)