PDA

View Full Version : Hexadecimal to ASCII UDF



Wendell
06-23-2007, 06:51 AM
Hello

I am no VBA person, and I really don't understand the syntax, but I am looking for a function that will convert a Hex string to its ASCII equivalent. I have searched the net for an appropriate UDF in VB that can be used to accomplish this simple task, I came up with the following:

Function dehex(str As String) As String
Dim I As Long
For I = 1 To Len(str) Step 2
dehex = dehex & Chr( _
InStr(1, "0123456789ABCDEF", Mid(str, I, 1)) * 16 - 16 _
+ InStr(1, "0123456789ABCDEF", Mid(str, I + 1, 1)) - 1)
Next I
End Function

Unfortunately this did not convert the Hex string correctly when applied to the column in Excel e.g.

Hex string: 43616c6c20206d65

Converted ASCII String: Ca__ _e

In ASCII this should read as: Call me

Please can anyone who knows VB take a look and make some suggestions...

Wendell:banghead:

mikerickson
06-23-2007, 08:44 AM
InStr is case sensitive. Also there is a VB function Str, so I changed that variable name. This should do it.
Function dehex(ByVal inputString As String) As String
Dim i As Long
inputString = UCase(inputString)
For i = 1 To Len(inputString) Step 2
dehex = dehex & Chr( _
InStr(1, "0123456789ABCDEF", Mid(inputString, i, 1)) * 16 - 16 _
+ InStr(1, "0123456789ABCDEF", Mid(inputString, i + 1, 1)) - 1)
Next i
End Function

Wendell
06-23-2007, 09:51 AM
Thank you very much mikerickson

I just tested the code and it worked just fine.

I knew there was someone out there that could help. My workaround was to use the Upper function then apply the dehex UDF. Anyways your suggestion is shorter.

Thanks Again

Wendell:beerchug:

mikerickson
06-23-2007, 10:27 AM
You're welcome.