• Welcome to Valhalla Legends Archive.
 

String & hex convershions

Started by Tontow, August 04, 2005, 08:08 PM

Previous topic - Next topic

Tontow

What are the differrent methods for converting string to hex and vice versa?

I found:
http://forum.valhallalegends.com/phpbbs/index.php?topic=12151.0
Quote
Here's a function I wrote to convert from String to HEX:

Public Function ToHex(ByVal strString As String) As String
Dim A&, strOut$, strC$
For A = 1 To Len(strString)
    If Len(Hex(Asc(Mid(strString, A, 1)))) = 1 Then
       strOut = strOut & " " & "0" & Hex(Asc(Mid(strString, A, 1)))
    Else
       strOut = strOut & " " & Hex(Asc(Mid(strString, A, 1)))
    End If
Next A
ToHex = strOut
End Function


What this function does is goes through every single character of a string, get's the ASCII value of it, then converts it to HEX using Visual Basic's Hex() function. If the length of the HEX value of the ASCII value is only 1, it adds a 0 before it to make it valid. (Byte)

This function does take quite a bit of lag to execute, so I wouldn't suggest using this very often.

Here's a function I wrote to convert from HEX to String formats.

Public Function ToStr(ByVal strString As String) As String
strString = Replace(strString, " ", "")
Dim A&, strOut$, strC$
For A = 1 To Len(strString) Step 2
       strOut = strOut & Chr(Val("&H" & Mid(strString, A, 2)))
Next A
ToStr = strOut
End Function


This function goes through every byte of a hex string, gets the character value of it, and returns a full line in String format.

But, I figured that there are probabbly other ways wich may be better.

Spht

Quote from: Tontow on August 04, 2005, 08:08 PM
What are the differrent methods for converting string to hex and vice versa?

I found:
http://forum.valhallalegends.com/phpbbs/index.php?topic=12151.0
Quote
Here's a function I wrote to convert from String to HEX:

Public Function ToHex(ByVal strString As String) As String
Dim A&, strOut$, strC$
For A = 1 To Len(strString)
    If Len(Hex(Asc(Mid(strString, A, 1)))) = 1 Then
       strOut = strOut & " " & "0" & Hex(Asc(Mid(strString, A, 1)))
    Else
       strOut = strOut & " " & Hex(Asc(Mid(strString, A, 1)))
    End If
Next A
ToHex = strOut
End Function


What this function does is goes through every single character of a string, get's the ASCII value of it, then converts it to HEX using Visual Basic's Hex() function. If the length of the HEX value of the ASCII value is only 1, it adds a 0 before it to make it valid. (Byte)

This function does take quite a bit of lag to execute, so I wouldn't suggest using this very often.

Here's a function I wrote to convert from HEX to String formats.

Public Function ToStr(ByVal strString As String) As String
strString = Replace(strString, " ", "")
Dim A&, strOut$, strC$
For A = 1 To Len(strString) Step 2
       strOut = strOut & Chr(Val("&H" & Mid(strString, A, 2)))
Next A
ToStr = strOut
End Function


This function goes through every byte of a hex string, gets the character value of it, and returns a full line in String format.

But, I figured that there are probabbly other ways wich may be better.

Could you be more specific and let us know what "method" you're looking for?

The Hex function returns a String representing the hexadecimal value of a number.  The Asc function returns an Integer representing the character code corresponding to the first letter in a string.  And the Chr function returns a String containing the character associated with the specified character code.

Those are the 3 basic functions you'll be using for this.  Try some experimenting yourself.

If you just want a raw hex dump, here's a basic function from General.bas along with a function which converts the string back from hex also, with no error checking.

Public Function StringToRawHex(ByVal Text As String) As String
    Dim i As Long
    For i = 1 To Len(Text)
        StringToRawHex = StringToRawHex & Right("00" & Hex(Asc(Mid(Text, i, 1))), 2)
    Next i
End Function

Public Function RawHexToString(ByVal Text As String) As String
    Dim i As Long
    For i = 1 To Len(Text) Step 2
        RawHexToString = RawHexToString & Chr("&H" & Mid(Text, i, 2))
    Next i
End Function


You might also want to check out Grok's DebugOutput function.

Yegg

Public Function StrToHex(ByVal String1 As String) As String
On Error Resume Next
Dim strTemp As String, strReturn As String, i As Long
For i = 1 To Len(String1)
    strTemp = Hex(Asc(Mid(String1, i, 1)))
    If Len(strTemp) = 1 Then strTemp = "0" & strTemp
    strReturn = strReturn & " " & strTemp
Next i
StrToHex = strReturn
End Function



Public Function HexToStr(ByVal Hex1 As String) As String
On Error Resume Next
Dim strTemp As String, strReturn As String, i As Long
If Len(Hex1) Mod 2 <> 0 Then Exit Function
For i = 1 To Len(Hex1) Step 2
    strReturn = strReturn & Chr(Val("&H" & Mid(Hex1, i, 2)))
Next i
HexToStr = strReturn
End Function

Tontow

Thanks.  What I was asking about was other ways to acheve the same thing as Grok's DebugOutput function without the string off to the side, I thinkI saw it done in one line on the bot develobment forum, but I haven't been able to find it agen.

R.a.B.B.i.T

Remove the parts that add them then.

Grok

This should work, not tested though.


    For x1 = 0 To ((iLen - 1) \ 16)
        sOffset = Right$("0000" & Hex(Offset), 4)
        sB = String(48, " ")
        sT = "................"
        For y1 = 1 To 16
            iPos = 16 * x1 + y1
            If iPos > iLen Then Exit For
            Mid(sB, 3 * (y1 - 1) + 1, 2) = Right("00" & Hex(Asc(Mid(sIn, iPos, 1))), 2) & " "
        Next y1
        If Len(sOut) > 0 Then sOut = sOut & vbCrLf
        sOut = sOut & sOffset & ":  "
        sOut = sOut & sB
        Offset = Offset + 16
    Next x1
    DebugOutput = sOut
End Function