Convert Hex Characters to ASCII Characters using EXCEL

Viewed 1987

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

3 Answers

How about:

Public Function KonvertHex(s As String) As String
    KonvertHex = ""
    For i = 1 To Len(s)
        KonvertHex = KonvertHex & Asc(Mid(s, i, 1))
    Next i
End Function

enter image description here

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
Related