问题:有没有更好的方法来做到这一点?guid to 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;
}
删除Base64编码的标准特殊字符的任何特殊原因? – Hemant 2009-06-23 12:56:46
是否有特殊原因需要对其进行编码? GUID中的字符都不需要编码URL或属性。 – blowdart 2009-06-23 12:56:54
@Hemant,因为对于URL,+和/和=在GET中效果不佳, @blowdart,使url更小 – Fredou 2009-06-23 12:59:33