Author | Message | Time |
---|---|---|
Tontow | What are the differrent methods for converting string to hex and vice versa? I found: https://davnit.net/bnet/vL/phpbbs/index.php?topic=12151.0 [quote] Here’s a function I wrote to convert from String to HEX: [code]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[/code] 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. [code]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[/code] This function goes through every byte of a hex string, gets the character value of it, and returns a full line in String format. [/quote] But, I figured that there are probabbly other ways wich may be better. | August 5, 2005, 1:08 AM |
Spht | [quote author=Tontow link=topic=12436.msg123105#msg123105 date=1123204133] What are the differrent methods for converting string to hex and vice versa? I found: https://davnit.net/bnet/vL/phpbbs/index.php?topic=12151.0 [quote] Here’s a function I wrote to convert from String to HEX: [code]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[/code] 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. [code]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[/code] This function goes through every byte of a hex string, gets the character value of it, and returns a full line in String format. [/quote] But, I figured that there are probabbly other ways wich may be better. [/quote] 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. [code]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[/code] You might also want to check out Grok's DebugOutput function. | August 7, 2005, 3:19 AM |
Yegg | [code]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[/code] [code] 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[/code] | August 7, 2005, 6:22 PM |
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. | August 10, 2005, 6:50 AM |
R.a.B.B.i.T | Remove the parts that add them then. | August 10, 2005, 5:10 PM |
Grok | This should work, not tested though. [code] 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 [/code] | August 11, 2005, 2:48 AM |