Jump to content
Sign in to follow this  
JSThePatriot

_StrToHex("String") and _HexToStr("Hex")

Recommended Posts

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
EndFunc

An 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
EndFunc

An 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 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)

Share this post


Link to post
Share on other sites

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.
  :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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 :idiot:.

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)

Share this post


Link to post
Share on other sites

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.
  :)

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.... :idiot:

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.
  :)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...