相关疑难解决方法(0)

将UUID存储为base64字符串

我一直在尝试使用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)

java sql uuid base64 bytearray

70
推荐指数
5
解决办法
7万
查看次数

标签 统计

base64 ×1

bytearray ×1

java ×1

sql ×1

uuid ×1