[VB.NET]ASCII String與Hex String的互轉

ASCII要轉Hex,可透過ToString函式帶入”X2”,或是用Hex函式。因此ASCII String轉Hex String可寫成:

    Public Function AsciiStringToHexString(ByVal asciiString As String) As String
Dim ascii() As Byte = System.Text.Encoding.Default.GetBytes(asciiString)
Dim count As Integer = ascii.Length
Dim hexArray(count - 1) As String
For idx As Integer = 0 To count - 1
hexArray(idx) = ascii(idx).ToString(“x2”)
Next
Return String.Join(“ “, hexArray)
End Function



而Hex要轉ASCII,可在前面帶入”&h”字串,轉成int後再轉為char。因此Hex String轉ASCII String可寫成:



    Public Function HexStringToAsciiString(ByVal hexString As String) As String
Dim array() As String = hexString.Split(New Char() {“ “c}, StringSplitOptions.RemoveEmptyEntries)
For idx As Integer = 0 To array.Length - 1
array(idx) = Chr(CInt(String.Format(“&h{0}”, array(idx))))
Next
Return String.Join(String.Empty, array)
End Function



程式範例

範例介面

image

完整範例如下:

Public Class Form1

Public Function AsciiStringToHexString(ByVal asciiString As String) As String
Dim ascii() As Byte = System.Text.Encoding.Default.GetBytes(asciiString)
Dim count As Integer = ascii.Length
Dim hexArray(count - 1) As String
For idx As Integer = 0 To count - 1
hexArray(idx) = ascii(idx).ToString(“x2”)
Next
Return String.Join(“ “, hexArray)
End Function


Public Function HexStringToAsciiString(ByVal hexString As String) As String
Dim array() As String = hexString.Split(New Char() {“ “c}, StringSplitOptions.RemoveEmptyEntries)
For idx As Integer = 0 To array.Length - 1
array(idx) = Chr(CInt(String.Format(“&h{0}”, array(idx))))
Next
Return String.Join(String.Empty, array)
End Function


Private Sub tbxASCII_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxASCII.TextChanged
If Me.ActiveControl Is sender Then
tbxHex.Text = AsciiStringToHexString(tbxASCII.Text)
End If
End Sub

Private Sub tbxHex_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxHex.TextChanged
If Me.ActiveControl Is sender Then
tbxASCII.Text = HexStringToAsciiString(tbxHex.Text)
End If
End Sub
End Class