.NET Short Unique Identifier

前端 未结 23 1152
忘掉有多难
忘掉有多难 2020-12-07 11:27

I need a unique identifier in .NET (cannot use GUID as it is too long for this case).

Do people think that the algorithm used here is a good candidate or do you have

23条回答
  •  鱼传尺愫
    2020-12-07 12:10

        public static string ToTinyUuid(this Guid guid)
        {
            return Convert.ToBase64String(guid.ToByteArray())[0..^2]  // remove trailing == padding 
                .Replace('+', '-')                          // escape (for filepath)
                .Replace('/', '_');                         // escape (for filepath)
        }
    

    Usage

    Guid.NewGuid().ToTinyUuid()
    

    It's not rocket science to convert back, so I'll leave you that much.

提交回复
热议问题