954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HEX to ASCII

I want to send some packets they are in hex format
01 00 00 00 AA 00 00 00 01
because how winsock works i first need to convert it to ascii right?
how could i do that every time i try it looses the NULLS - 00
help would be appreciated thanks...

using vb6 btw
this is the script i use to convert hex to ascii

Private Function HexToString(Value As String)
    Dim szTemp As String
    szTemp = Value
    
    Dim szData As String
    szData = ""
    While Len(szTemp) > 0
        szData = Chr(CLng("&h" & Right(szTemp, 2))) & szData
        If (Len(szTemp) = 1) Then
            szTemp = Left(szTemp, Len(szTemp) - 1)
        Else
            szTemp = Left(szTemp, Len(szTemp) - 2)
        End If
    Wend
    HexToString = szData
End Function
lttleastig
Junior Poster in Training
51 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

Try the following (shorter) version to convert your hex to ascii -

Public Function hex2ascii(ByVal hextext As String) As String
    
For y = 1 To Len(hextext)
    num = Mid(hextext, y, 2)
    Value = Value & Chr(Val("&h" & num))
    y = y + 1
Next y

hex2ascii = Value
End Function


In your form it can be called -

Text1.Text = hex2ascii (TheHexValueHere)
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

thanks for reply error variable not defined then it highlights "y" where y = 1

lttleastig
Junior Poster in Training
51 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

nvm the above post code works great only thing is when i insert
01 00 00 00 AA 00 00 00 01
it adds more than the correct amount of nulls
butif i enter it like
01000000AA00000001
without spaces then it outputs the correct value

lttleastig
Junior Poster in Training
51 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 
error variable not defined then it highlights "y" where y = 1

My apologies, add the following BEFORE the code is executed -

Dim y As Integer
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 
nvm the above post code works great only thing is when i insert 01 00 00 00 AA 00 00 00 01 it adds more than the correct amount of nulls butif i enter it like 01000000AA00000001 without spaces then it outputs the correct value

That is because it reads the empty space as a zero. Remove all the spaces first and then run the conversion -

Text1.Text = Replace(Text1.Text," ","")
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Thanks everything working perfectly now

lttleastig
Junior Poster in Training
51 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

Its only a pleasure. Happy coding.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Ow yeah fixed problem by putting the output into a string then sending it straight from the string...
bec bedfore i stored the converted ascii into a textbox then send it from there but turns out that doesnt work too well :D

lttleastig
Junior Poster in Training
51 posts since Nov 2010
Reputation Points: 14
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You