你需要使用Convert.FromBase64String
method的base64编码字符串先转换成字节数组。
然后,您可以获取该字节数组,并使用接受格式说明符的过载Byte.ToString
分别将它们转换为等效的十六进制表示。可用的格式说明符记录为here,但您要的是X
or x
for hexadecimal,具体取决于您是否要将字母大写。
如果您想将其全部隐藏起来以便于使用,您可以将其全部封装为String
类的扩展方法。例如:
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ConvertToHex(str As String) As String
' Convert the specified base64-encoded string into an array of bytes.
Dim bytes As Byte() = Convert.FromBase64String(str)
' Loop through each of the items in the array and convert
' to their equivalent hexadecimal representations
Dim sb As New StringBuilder()
For Each b As Byte In bytes
sb.Append(b.ToString("X2"))
Next
Return sb.ToString()
End Function
或者,你可以调查使用的BitConverter.ToString
method而不是循环到每个值转换为字节数组为等效的十六进制字符串表示英寸这将产生一个连字符划定的十六进制对。
我不知道哪种方法“更好”或更高性能。您必须对代码进行概要分析,或者选择更具可读性的那一个。
<System.Runtime.CompilerServices.Extension> _
Public Shared Function ConvertToHex(str As String) As String
' Convert the specified base64-encoded string into an array of bytes.
Dim bytes As Byte() = Convert.FromBase64String(str)
' Convert each of the items in the array to a hex string.
return BitConverter.ToString(bytes)
End Function
如果你不想字符串中的破折号,该BitConverter.ToString
方法返回时,你就需要将其删除:
returnString.Replace("-", String.Empty)
http://msdn.microsoft.com/en- us/library/system.convert.frombase64string.aspx – 2011-12-19 14:01:14
Base64是无关紧要的,任何字符串都是一个字节数组,然后用一些格式化BitConverter.ToString()来完成这项工作。 – 2011-12-19 14:06:50