字串及16進位值互轉
這段程式碼是很久之前在網路上找的,來源已經找不到@@,記錄一下。
Public Function StrToHex(ByVal str As String) As String
Dim buf() As Byte = System.Text.Encoding.Default.GetBytes(str)
Dim result As String = ""
For i As Integer = 0 To buf.Length - 1
result += String.Format("{0:X}{1:X}", buf(i) \ &H10, buf(i) Mod &H10)
Next
Return result
End Function
''' <summary>16進位轉字串</summary>
Public Function HexToStr(ByVal str As String) As String
Dim b, t As Integer
Dim result As String = ""
For i As Integer = 0 To str.Length - 1
t = Integer.Parse(str(i), Globalization.NumberStyles.AllowHexSpecifier)
If i Mod 2 = 0 Then
b = t
Else
b = b * &H10 + t
result += Convert.ToChar(b)
End If
Next
Return result
End Function
若要將16進位值轉為10進位值也可以這樣寫
Dim IntValue As Integer = Integer.Parse(Str, Globalization.NumberStyles.AllowHexSpecifier)