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
C#
public string GuidToBase64(Guid guid)
{
return Convert.ToBase64String(guid.ToByteArray()).Replace("/", "-").Replace("+", "_").Replace("=", "");
}
public Guid Base64ToGuid(string base64)
{
Guid guid = default(Guid);
base64 = base64.Replace("-", "/").Replace("_", "+") + "==";
try {
guid = new Guid(Convert.FromBase64String(base64));
}
catch (Exception ex) {
throw new Exception("Bad Base64 conversion to GUID", ex);
}
return guid;
}
Question: is there a better way to do that?
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
C#
public string GuidToBase64(Guid guid)
{
return Convert.ToBase64String(guid.ToByteArray()).Replace("/", "-").Replace("+", "_").Replace("=", "");
}
public Guid Base64ToGuid(string base64)
{
Guid guid = default(Guid);
base64 = base64.Replace("-", "/").Replace("_", "+") + "==";
try {
guid = new Guid(Convert.FromBase64String(base64));
}
catch (Exception ex) {
throw new Exception("Bad Base64 conversion to GUID", ex);
}
return guid;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可能想查看此网站: http://prettycode.org/2009/11/12/short-guid/
它看起来非常接近你正在做的事情。
You might want to check out this site: http://prettycode.org/2009/11/12/short-guid/
It looks very close to what you're doing.
使用此技术格式化 GUID 以在 URL 或文件名中使用的一个问题是,两个不同的 GUID 可能会生成两个仅大小写不同的值,例如:
由于 URL 有时被解释为不区分大小写,并且在 Windows 文件路径中并且文件名不区分大小写。 这可能会导致碰撞。
One problem with using this technique to format a GUID for use in a URL or filename is that two distinct GUIDs can produce two values that differ only in case, e.g.:
Since URLs are sometimes interpreted as being case-insensitive, and in Windows file paths and filenames are case-insensitive. this could lead to collisions.
我知道您最终剪裁 == 的原因是因为您可以确定对于 GUID(16 字节),编码的字符串总是以 == 结尾。 所以每次转换可以节省2个字符。
除了 @Skurmedal 已经提到的点(在输入无效字符串的情况下应该抛出异常),我认为您发布的代码已经足够好了。
I understand that the reason you are clipping == in the end is that because you can be certain that for GUID (of 16 bytes), encoded string will always end with ==. So 2 characters can be saved in every conversion.
Beside the point @Skurmedal already mentioned (should throw an exception in case of invalid string as input), I think the code you posted is just good enough.
在 .NET Core 中,您可以使用 Span 以获得更好的性能并且无需内存分配。
有关使用 对 guid 进行 Base64 编码的高性能技术,以及非常好的视频说明。
In
.NET Core
you can use Spans for better performance and no memory allocation.For more info read about using high-performance techniques to base64 encode a guid, and a very nice video explanation.
如果您的方法无法将传递给它的 Base64 转换为 GUID,您不应该抛出异常吗? 传递给该方法的数据显然是错误的。
If your method cannot convert the Base64 passed to it to a GUID, shouldn't you throw an exception? The data passed to the method is clearly erronous.
我正在使用一种方法来编码和缩短我的 URL(Guid): https://dotnetfiddle.net /iQ7nGv
There is a method that I am using to encode as well as shorten for my URL (Guid): https://dotnetfiddle.net/iQ7nGv