相关疑难解决方法(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万
查看次数

guid到base64,用于URL

问题:有更好的方法吗?

VB.Net

Function GuidToBase64(ByVal guid As Guid) As String
    Return Convert.ToBase64String(guid.ToByteArray).Replace("/", "-").Replace("+", "_").Replace("=", "")
End Function

Function Base64ToGuid(ByVal base64 As String) As Guid
    Dim guid As Guid
    base64 = base64.Replace("-", "/").Replace("_", "+") & "=="

    Try
        guid = New Guid(Convert.FromBase64String(base64))
    Catch ex As Exception
        Throw New Exception("Bad Base64 conversion to GUID", ex)
    End Try

    Return guid
End Function
Run Code Online (Sandbox Code Playgroud)

C#

public string GuidToBase64(Guid guid)
{
    return Convert.ToBase64String(guid.ToByteArray()).Replace("/", "-").Replace("+", "_").Replace("=", "");
}

public Guid Base64ToGuid(string base64)
{
   Guid guid = default(Guid);
   base64 = …
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net base64 guid

45
推荐指数
3
解决办法
3万
查看次数

标签 统计

base64 ×2

.net ×1

bytearray ×1

c# ×1

guid ×1

java ×1

sql ×1

uuid ×1

vb.net ×1