I am using the following function to convert ascii to hex, but it is not working properly.

Private Function HexString(EvalString As String) As String
Dim intStrLen As Integer
Dim intLoop As Integer
Dim strHex As String

EvalString = Trim(EvalString)
intStrLen = Len(EvalString)
For intLoop = 1 To intStrLen
strHex = strHex & " " & Hex(Asc(Mid(EvalString, intLoop, 1)))
Next
HexString = strHex
End Function

What am I doing wrong here?

In what way it's not working properly?

I tried:

Dim strOut As String
strOut = HexString("ABC")

and strOut was " 41 42 43" which is correct.

well, its printing 0 instead of 00, 1 instead of 01, 2 instead of 02......and so on
i wonder why?

I see. Here's a Q&D solution:

Private Function HexString(EvalString As String) As String
Dim intStrLen As Integer
Dim intLoop As Integer
Dim strHex As String

EvalString = Trim(EvalString)
intStrLen = Len(EvalString)
For intLoop = 1 To intStrLen
strHex = strHex & " " & Left$("0" & Hex(Asc(Mid(EvalString, intLoop, 1))), 2)
Next
HexString = strHex
End Function

I see. Here's a Q&D solution:

Private Function HexString(EvalString As String) As String
Dim intStrLen As Integer
Dim intLoop As Integer
Dim strHex As String

EvalString = Trim(EvalString)
intStrLen = Len(EvalString)
For intLoop = 1 To intStrLen
strHex = strHex & " " & Left$("0" & Hex(Asc(Mid(EvalString, intLoop, 1))), 2)
Next
HexString = strHex
End Function

There's one problem with this, now it adds zero to every single pair of digits, Like ff is now 0f, a1 is now 01.... and so on!

Sorry :sweat: Right$ instead of Left$...

Private Function HexString(EvalString As String) As String
Dim intStrLen As Integer
Dim intLoop As Integer
Dim strHex As String

EvalString = Trim(EvalString)
intStrLen = Len(EvalString)
For intLoop = 1 To intStrLen
strHex = strHex & " " & Right$("0" & Hex(Asc(Mid(EvalString, intLoop, 1))), 2)
Next
HexString = strHex
End Function
Comments
Thanx!!!!

COOOOOOOL!!!!
Many Thanx For your Reply!

Now its working perfect!

hi all,
i see there's a symbol "$" following the Right function

strHex = strHex & " " & Right$("0" & Hex(Asc(Mid(EvalString, intLoop, 1))), 2)

would you mind telling me what's it meaning?

thanks a lot!

PS : i'm new in VB

This question has already been answered. Start a new discussion instead.