0

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?

3
Contributors
8
Replies
17
Views
8 Years
Discussion Span
Last Post by Teme64
Featured Replies
  • 1

    Sorry :sweat: Right$ instead of Left$... [CODE=VB]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) … Read More

0

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.

0

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

0

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
0

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!

1

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
Votes + Comments
Thanx!!!!
0

COOOOOOOL!!!!
Many Thanx For your Reply!

Now its working perfect!

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.