JSThePatriot Posted January 11, 2005 Share Posted January 11, 2005 (edited) Below is the code. Let me know if you have any questions on either of the functions._StrToHex("String");=============================================================================== ; ; Function Name: _StrToHex("string") ; Description: Convert a string of characters to hexidecimal. ; Parameter(s): $strChar is the string you want to convert. ; Requirement(s): String Input. ; Return Value(s): On Success - Returns the converted string in hexidecimal. ; On Failure - -1 and sets @ERROR = 1 ; Author(s): Jarvis Stubblefield ; ;=============================================================================== Func _StrToHex($strChar) Local $aryChar, $i, $iDec, $hChar, $file, $strHex $aryChar = StringSplit($strChar, "") For $i = 1 To $aryChar[0] $iDec = Asc($aryChar[$i]) $hChar = Hex($iDec, 2) $strHex = $strHex & $hChar Next If $strHex = "" Then SetError(1) Return -1 Else Return $strHex EndIf EndFuncAn Example:#include <StrToHex.au3> $String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex)_HexToStr("Hex");=============================================================================== ; ; Function Name: _HexToStr("hex") ; Description: Convert a hex string of characters to ASCII Characters. ; Parameter(s): $strHex is the hex string you want to convert. ; Requirement(s): Hex Input. ; Return Value(s): On Success - Returns the converted string of characters. ; On Failure - -1 and sets @ERROR = 1 ; Author(s): Jarvis Stubblefield ; ;=============================================================================== Func _HexToStr($strHex) Local $strChar, $aryHex, $i, $iDec, $Char, $file, $strHex $aryHex = StringSplit($strHex, "") For $i = 1 To $aryHex[0] $iOne = $aryHex[$i] $i = $i + 1 $iTwo = $aryHex[$i] $iDec = Dec($iOne & $iTwo) $Char = Chr($iDec) $strChar = $strChar & $Char Next If $strChar = "" Then SetError(1) Return -1 Else Return $strChar EndIf EndFuncAn Example:#include <HexToStr.au3> $Hex = "2030405060" $String = _HexToStr($Hex) MsgBox(0, "Hex", "Original Hex: " & $Hex & " String: " & $String)Edit: I fixed the problems. I am resubmitting this to you JdeB on the UDF page. (You will need to re-download the zip file).Thank you everyone for your opinions.JS Edited January 11, 2005 by JSThePatriot AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
Developers Jos Posted January 11, 2005 Developers Share Posted January 11, 2005 Think you have a logic error.... try this: $String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & _HextoStr($hex)) shouldn't the output of the second msgbox be the value of $string ? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
tuape Posted January 11, 2005 Share Posted January 11, 2005 Thank you everyone for your opinions.JS<{POST_SNAPBACK}>I'm not sure if I'm using these wrong but shouldn't a string converted to hex be convertable back to string?For example:$String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex); Works OK so far $String2 = _HexToStr($Hex) MsgBox(0, "Hex", "String: " & $String2 & " Hex: " & $Hex); This won't work, hex is -1 Link to comment Share on other sites More sharing options...
JSThePatriot Posted January 11, 2005 Author Share Posted January 11, 2005 Think you have a logic error.... try this:$String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & _HextoStr($hex))shouldn't the output of the second msgbox be the value of $string ?<{POST_SNAPBACK}>Yes my mistake there. I just copied and pasted and forgot to swap around the words... ::fixes it::JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
JSThePatriot Posted January 11, 2005 Author Share Posted January 11, 2005 I fixed the above named problems. Check the top post for code. The string for some reason wasnt returning the correct value. I modified my logic checking that and it works now. You can convert a string back and forth all day long .Enjoy.JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
Developers Jos Posted January 11, 2005 Developers Share Posted January 11, 2005 Yes my mistake there. I just copied and pasted and forgot to swap around the words... ::fixes it::JS<{POST_SNAPBACK}>But the point really is that it doesn't work.Just run this script with your functions and see the result of the second msgbox:It says -1!!$String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex) MsgBox(0, "Hex", "Original String: " & $String & " Convert twice: " & _HextoStr($hex)) SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
JSThePatriot Posted January 11, 2005 Author Share Posted January 11, 2005 But the point really is that it doesn't work.Just run this script with your functions and see the result of the second msgbox:It says -1!!$String = "This is a String" $Hex = _StrToHex($String) MsgBox(0, "Hex", "Original String: " & $String & " Hex: " & $Hex) MsgBox(0, "Hex", "Original String: " & $String & " Convert twice: " & _HextoStr($hex))<{POST_SNAPBACK}>I got that... fixed it. Check the code. It works. I had a logic error in my If statement that was supposed to return the result.Thanks,JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
Developers Jos Posted January 11, 2005 Developers Share Posted January 11, 2005 I got that... fixed it. Check the code. It works. I had a logic error in my If statement that was supposed to return the result.Thanks,JS<{POST_SNAPBACK}>Yes you are right ...its correct now.... SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now