excelvba

Convert Hex Characters to ASCII Characters using EXCEL


I have some code as shown below that i found on a forum , that will convert ASCII Code to Hexadecimal Characters using a VBA script, is it possible to convert Hex Characters to ASCII Characters ??

The code i have is as follows

 Sub AsciiToHex()
 Dim strg As String
 Dim tmp As String

 strg = Worksheets("Sheet1").Range("A1")
 Worksheets("Sheet1").Range("A5").value = strg

 tmp = ""
 For i = 1 To Len(strg)
 tmp = tmp & hex((Asc(Mid(strg, i, 1))))
 Next

 Worksheets("Sheet1").Range("A6").value = tmp

 End Sub

I have tried to to swap the hex((Asc(Mid(strg, i, 1)))) to Asc((hex(Mid(strg, i, 1)))) but that did not work. Any help would be appreciated

Sample Data

Hex Format 48 65 6C 6C 6F

After conversion would be the following

Ascii Format H e l l o


Solution

  • Hex To String

    Function HexToString(InitialString As String) As String
        Dim i As Long
        For i = 1 To Len(InitialString) Step 2
            HexToString = HexToString & Chr("&H" & (Mid(InitialString, i, 2)))
        Next i
    End Function
    
    Function StringToHex(InitialString As String) As String
        Dim i As Long
        For i = 1 To Len(InitialString)
            StringToHex = StringToHex & Hex(Asc(Mid(InitialString, i, 1)))
        Next i
    End Function