Sign in to follow this  
Followers 0
James

_StrToHex()

26 posts in this topic

#1 ·  Posted (edited)

Hi,

Just thought I would share my String To Hex function.

Speed:

I conducted some tests to see which was faster, expecting the _StringToHex() to win hands down. However the results suprised me!

My first test was to convert "AutoIt3" nice and simple. First I used _StringToHex() which converted at, 28.9279782765687 mine taking only, 0.722158821861438!

My second test was to convert "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam massa urna, ornare et, pharetra nec, ultrices quis, dui. Pellentesque nec lorem. Fusce pharetra eros. Curabitur varius pede non pede semper iaculis. Mauris vel eros. Aliquam volutpat laoreet purus. Sed tincidunt justo. Nulla sagittis. Nulla facilisi. Etiam volutpat nulla nec libero. Suspendisse sagittis velit eu leo. Donec mattis nisi et ligula. Suspendisse tempus laoreet erat.", again I tested out the _StringToHex() function first, converting the text at 176.176581101788. I tested it with mine and it converted at 24.5785428036245.

In conclusion mine is faster :D I use 512mb so it could be faster yet!

ConsoleWrite(_StrToHex("AutoIt3") & @CRLF)
 
 Func _StrToHex($hex)
     Local $strReturn
     For $i = 1 To StringLen($hex)
         $strTmp = Hex(Asc(StringMid($hex, $i, 1)), 2)
         If StringLen($strTmp) = 1 Then $strTmp = "0" & $strTmp
         $strReturn = $strReturn & $strTmp
     Next
     Return $strReturn
 EndFunc

Hope someone can make use of it here :D

James

Edit: Just noticed there is already a string to hex available :)

Edit 2: Mine is actually faster! Read top of this post

Edited by JamesB

Share this post


Link to post
Share on other sites



Hex(StringToBinary($str))


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Nice.

1) How did you test for speed?

2) Which one among those 3 above, is fastest?

Share this post


Link to post
Share on other sites

1) How did you test for speed?

2) Which one among those 3 above, is fastest?

1) Take a look at TimerInit() and TimerDiff(), if you need an example, just ask :D

2) Out of the first two, mine, the 3rd one from Siao, I don't know, you could test it when you have had a look at 1. :D

Thanks!

Share this post


Link to post
Share on other sites

Hex(StringToBinary($str))
Not sure why your using "Hex" there :D

These would be faster than the conventional:

$sText = "This is some text"
$sHex = __StringToHex($sText)
$sString = __HexToString($sHex)
ConsoleWrite(@CRLF & $sHex & @CRLF & $sString & @CRLF)

Func __StringToHex($sString)
    Return StringTrimLeft(StringToBinary($sString), 2)
EndFunc

Func __HexToString($sString)
    If StringLeft($sString, 2) <> "0x"Then
        $sString = "0x" & $sString
    EndIf
    Return BinaryToString($sString)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Do you know how fast that runs Smoke?

Fast!!! :D

Let's put it this way... Try opening an binary file (exe) with FileOpen/FileRead, and converting it with yours.

Now do the same thing with the funcs I posted.... <need I say more :D >


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

My results:

############# _StrToHex ###########
4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747565722061646970697363696E

6720656C69742E20457469616D206D617373612075726E612C206F726E6172652065742C207068617265747261206E65632C

20756C74726963657320717569732C206475692E2050656C6C656E746573717565206E6563206C6F72656D2E204675736365

2070686172657472612065726F732E20437572616269747572207661726975732070656465206E6F6E20706564652073656D

70657220696163756C69732E204D61757269732076656C2065726F732E20416C697175616D20766F6C7574706174206C616F

726565742070757275732E205365642074696E636964756E74206A7573746F2E204E756C6C612073616769747469732E204E

756C6C6120666163696C6973692E20457469616D20766F6C7574706174206E756C6C61206E6563206C696265726F2E205375

7370656E64697373652073616769747469732076656C6974206575206C656F2E20446F6E6563206D6174746973206E697369

206574206C6967756C612E2053757370656E64697373652074656D707573206C616F7265657420657261742E
7.30646451381842
##########################################
############# Hex(StringToBonary) ###########
4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747565722061646970697363696E

6720656C69742E20457469616D206D617373612075726E612C206F726E6172652065742C207068617265747261206E65632C

20756C74726963657320717569732C206475692E2050656C6C656E746573717565206E6563206C6F72656D2E204675736365

2070686172657472612065726F732E20437572616269747572207661726975732070656465206E6F6E20706564652073656D

70657220696163756C69732E204D61757269732076656C2065726F732E20416C697175616D20766F6C7574706174206C616F

726565742070757275732E205365642074696E636964756E74206A7573746F2E204E756C6C612073616769747469732E204E

756C6C6120666163696C6973692E20457469616D20766F6C7574706174206E756C6C61206E6563206C696265726F2E205375

7370656E64697373652073616769747469732076656C6974206575206C656F2E20446F6E6563206D6174746973206E697369

206574206C6967756C612E2053757370656E64697373652074656D707573206C616F7265657420657261742E
0.0220487872818799
##########################################
############# _StringToHex ###########
4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E7365637465747565722061646970697363696E

6720656C69742E20457469616D206D617373612075726E612C206F726E6172652065742C207068617265747261206E65632C

20756C74726963657320717569732C206475692E2050656C6C656E746573717565206E6563206C6F72656D2E204675736365

2070686172657472612065726F732E20437572616269747572207661726975732070656465206E6F6E20706564652073656D

70657220696163756C69732E204D61757269732076656C2065726F732E20416C697175616D20766F6C7574706174206C616F

726565742070757275732E205365642074696E636964756E74206A7573746F2E204E756C6C612073616769747469732E204E

756C6C6120666163696C6973692E20457469616D20766F6C7574706174206E756C6C61206E6563206C696265726F2E205375

7370656E64697373652073616769747469732076656C6974206575206C656F2E20446F6E6563206D6174746973206E697369

206574206C6967756C612E2053757370656E64697373652074656D707573206C616F7265657420657261742E
4.768913030502
##########################################
-----------------------------------


############# BinaryToString(Binary('0x'& $text)) ###########
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam massa urna, ornare et, pharetra nec, ultrices quis, dui. Pellentesque nec lorem. Fusce pharetra eros. Curabitur varius pede non pede semper iaculis. Mauris vel eros. Aliquam volutpat laoreet purus. Sed tincidunt justo. Nulla sagittis. Nulla facilisi. Etiam volutpat nulla nec libero. Suspendisse sagittis velit eu leo. Donec mattis nisi et ligula. Suspendisse tempus laoreet erat.
0.0279243518573788
##########################################
############# _HexToString ###########
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam massa urna, ornare et, pharetra nec, ultrices quis, dui. Pellentesque nec lorem. Fusce pharetra eros. Curabitur varius pede non pede semper iaculis. Mauris vel eros. Aliquam volutpat laoreet purus. Sed tincidunt justo. Nulla sagittis. Nulla facilisi. Etiam volutpat nulla nec libero. Suspendisse sagittis velit eu leo. Donec mattis nisi et ligula. Suspendisse tempus laoreet erat.
10.1346062396785
##########################################

--> Fastest way is Hex(StringToBinary($str))

and to revert: BinaryToString(Binary('0x'& $str))


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Here is my test:

#include <GUIConstants.au3>
#Include <GuiListView.au3>
#include <array.au3>
#include <String.au3>

Global $test = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam massa urna, ornare et, pharetra nec, ultrices quis, dui. Pellentesque nec lorem. Fusce pharetra eros. Curabitur varius pede non pede semper iaculis. Mauris vel eros. Aliquam volutpat laoreet purus. Sed tincidunt justo. Nulla sagittis. Nulla facilisi. Etiam volutpat nulla nec libero. Suspendisse sagittis velit eu leo. Donec mattis nisi et ligula. Suspendisse tempus laoreet erat."

#region Variablen definieren
Global $ZeitErgebnisse = ObjCreate("System.Collections.ArrayList")
If @error Then
    MsgBox(0, "", "Fehler bei der Objekt-Erstellung")
    Exit
EndIf

Global $TimerDiffTime
Global $Name, $Zeitsumme, $Zeitquadratsumme, $ZeitPunkt, $Zeit, $TempArray[3], $N, $X
Global $GUI, $ListView, $Button, $eins, $e, $RahmenZeit
#endregion

;------------------------------------------------------------------------------------------
;Anzahl der Messdurchläufe angeben - je höher N umso genauer wird die gemessene Zeitangabe:
Global $N = 3000
;------------------------------------------------------------------------------------------


#region Zeitaufwand der Methode bestimmen
$Name = "_StrToHex" 
$Zeitquadratsumme = 0
$Zeitsumme = 0
SplashTextOn("Test der Methode:", @CRLF & $Name, 250, 60)

For $XXX = 1 To $N
    $ZeitPunkt = TimerInit()
    
    ;---------Code-----------
$result = _StrToHex($test)
    ;------------------------
    $Zeit = TimerDiff($ZeitPunkt)
    
    $Zeitsumme += $Zeit
    $Zeitquadratsumme += $Zeit ^ 2
Next
$TempArray[0] = $Name
$TempArray[1] = $Zeitsumme / $N
$TempArray[2] = Sqrt((($N * $Zeitquadratsumme) - ($Zeitsumme ^ 2)) / ($N * ($N - 1)))
$ZeitErgebnisse.add ($TempArray)
SplashOff()
#endregion


#region Zeitaufwand der Methode bestimmen
$Name = "__StringToHex($test) (Binary)" 
$Zeitquadratsumme = 0
$Zeitsumme = 0
SplashTextOn("Test der Methode:", @CRLF & $Name, 250, 60)

For $XXX = 1 To $N
    $ZeitPunkt = TimerInit()
    
    ;---------Code-----------
$2 = __StringToHex($test)
    ;------------------------
    $Zeit = TimerDiff($ZeitPunkt)
    
    $Zeitsumme += $Zeit
    $Zeitquadratsumme += $Zeit ^ 2
Next
$TempArray[0] = $Name
$TempArray[1] = $Zeitsumme / $N
$TempArray[2] = Sqrt((($N * $Zeitquadratsumme) - ($Zeitsumme ^ 2)) / ($N * ($N - 1)))
$ZeitErgebnisse.add ($TempArray)
SplashOff()
#endregion


#region Zeitaufwand der Methode bestimmen
$Name = "_StringToHex($test)" 
$Zeitquadratsumme = 0
$Zeitsumme = 0
SplashTextOn("Test der Methode:", @CRLF & $Name, 250, 60)

For $XXX = 1 To $N
    $ZeitPunkt = TimerInit()
    
    ;---------Code-----------
$3 = _StringToHex($test)
    ;------------------------
    $Zeit = TimerDiff($ZeitPunkt)
    
    $Zeitsumme += $Zeit
    $Zeitquadratsumme += $Zeit ^ 2
Next
$TempArray[0] = $Name
$TempArray[1] = $Zeitsumme / $N
$TempArray[2] = Sqrt((($N * $Zeitquadratsumme) - ($Zeitsumme ^ 2)) / ($N * ($N - 1)))
$ZeitErgebnisse.add ($TempArray)
SplashOff()
#endregion



#region Zeitaufwand der Methode bestimmen
$Name = "_HexToString($1)" 
$Zeitquadratsumme = 0
$Zeitsumme = 0
SplashTextOn("Test der Methode:", @CRLF & $Name, 250, 60)

For $XXX = 1 To $N
    $ZeitPunkt = TimerInit()
    
    ;---------Code-----------
    $1 = _HexToString($result)
    ;------------------------
    $Zeit = TimerDiff($ZeitPunkt)
    
    $Zeitsumme += $Zeit
    $Zeitquadratsumme += $Zeit ^ 2
Next
$TempArray[0] = $Name
$TempArray[1] = $Zeitsumme / $N
$TempArray[2] = Sqrt((($N * $Zeitquadratsumme) - ($Zeitsumme ^ 2)) / ($N * ($N - 1)))
$ZeitErgebnisse.add ($TempArray)
SplashOff()
#endregion


#region Zeitaufwand der Methode bestimmen
$Name = "__HexToString($result) (binary)" 
$Zeitquadratsumme = 0
$Zeitsumme = 0
SplashTextOn("Test der Methode:", @CRLF & $Name, 250, 60)

For $XXX = 1 To $N
    $ZeitPunkt = TimerInit()
    
    ;---------Code-----------
    $2 = __HexToString($result)
    ;------------------------
    $Zeit = TimerDiff($ZeitPunkt)
    
    $Zeitsumme += $Zeit
    $Zeitquadratsumme += $Zeit ^ 2
Next
$TempArray[0] = $Name
$TempArray[1] = $Zeitsumme / $N
$TempArray[2] = Sqrt((($N * $Zeitquadratsumme) - ($Zeitsumme ^ 2)) / ($N * ($N - 1)))
$ZeitErgebnisse.add ($TempArray)
SplashOff()
#endregion




#region Auswerten und Vergleichen
;~ _ArrayListSort2Dim($ZeitErgebnisse, 1, 0)

$eins = $ZeitErgebnisse.Item (0)

$GUI = GUICreate("Ergebnisse", 633, 307, 193, 115)
$ListView = GUICtrlCreateListView("Name|Zeit [ms]|Verhältnis|Standardabweichung [ms]|rel. Abweichung", 0, 0, 632, 265)
$Button = GUICtrlCreateButton("O.K.", 240, 272, 121, 33, 0)

_GUICtrlListView_SetColumnWidth ($ListView, 0, 203)
_GUICtrlListView_SetColumnWidth ($ListView, 1, 100)
_GUICtrlListView_SetColumnWidth ($ListView, 2, 75)
_GUICtrlListView_SetColumnWidth ($ListView, 3, 150)
_GUICtrlListView_SetColumnWidth ($ListView, 4, 100)

For $e In $ZeitErgebnisse
    GUICtrlCreateListViewItem($e[0] & '|' & Round($e[1], 3) & '|' & Round($e[1] / $eins[1], 1) & '|' & Round($e[2], 4) & '|' & Int(100 * $e[2] / $e[1]) & '%', $ListView)
Next

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            Exit
    EndSwitch
WEnd
#endregion

;Sortiert eine ArrayList mit enthaltenem Array nach einem Index dieses Arrays im Selection-Sort-Algorithmus
;Author: AspirinJunkie@german-nlite.de
;~ Func _ArrayListSort2Dim(ByRef $ArrayList, $DimIndex, $Descending = 0)
;~     Local $minIndex[2], $SortValue

;~     If Not IsObj($ArrayList) Then Return SetError(1, 1, 0)
;~     If $ArrayList.count = 0 Then Return SetError(2, 2, 0)
;~     
;~     $SortValue = $ArrayList.Item (0)
;~     If UBound($SortValue) <= $DimIndex Then Return SetError(3, 3, 0)
;~     
;~     If $ArrayList.count < 2 Then Return SetError(4, 4, 0)
;~     
;~     For $i = 0 To $ArrayList.count - 1
;~         $SortValue = $ArrayList.Item ($i)
;~         $SortValue = $SortValue[$DimIndex]

;~         $minIndex[0] = $i
;~         $minIndex[1] = $SortValue

;~         For $e = $i + 1 To $ArrayList.count - 1
;~             $SortValue = $ArrayList.Item ($e)
;~             $SortValue = $SortValue[$DimIndex]

;~             If $SortValue < 


Func _StrToHex($hex)
     Local $strReturn
     For $i = 1 To StringLen($hex)
         $strTmp = Hex(Asc(StringMid($hex, $i, 1)), 2)
         If StringLen($strTmp) = 1 Then $strTmp = "0" & $strTmp
         $strReturn = $strReturn & $strTmp
     Next
     Return $strReturn
EndFunc


Func __StringToHex($sString)
    Return StringTrimLeft(StringToBinary($sString), 2)
EndFunc

Func __HexToString($sString)
    If StringLeft($sString, 2) <> "0x"Then
        $sString = "0x" & $sString
    EndIf
    Return BinaryToString($sString)
EndFunc

Its the script from http://www.autoit.de/index.php?page=Thread&postID=39971

Posted Image


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Well, I am slow then :D

I don't know exactly how Jon did the StringToBinary or the BinaryToString, but the fastest way I can think of is to do a Static Array of all 256 Hex values example:

$sText = "This is a Test."
ConsoleWrite(@CRLF & _StringToHexAUT($sText) & @CRLF)

Func _StringToHexAUT($sStr)
    Local $aHex[256] = _
                ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09",  _
                "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12", "13",  _
                "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D",  _
                "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27",  _
                "28", "29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31",  _
                "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B",  _
                "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45",  _
                "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F",  _
                "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",  _
                "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63",  _
                "64", "65", "66", "67", "68", "69", "6A", "6B", "6C", "6D",  _
                "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77",  _
                "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80", "81",  _
                "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B",  _
                "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95",  _
                "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",  _
                "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9",  _
                "AA", "AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3",  _
                "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD",  _
                "BE", "BF", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7",  _
                "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1",  _
                "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB",  _
                "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5",  _
                "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", "EF",  _
                "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9",  _
                "FA", "FB", "FC", "FD", "FE", "FF"]
    
    Local $szHoldVar = "", $aSplit = StringSplit($sStr, ""), $i
    For $i = 1 To $aSplit[0]
        $szHoldVar &= $aHex[Asc($aSplit[$i])]
    Next
    Return $szHoldVar
EndFunc

Now, this will be no way shape or form as fast as the internal ones, but I'm curious if it would at least keep up with the others.

I know this method in the other languages I use is the closest I can get to to Jon's speed of the StringToBinary/BinaryToString... Of course, they don't have the need to split or Asc the chars either.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

See that could be slower as it has to keep exchanging values all the time.

:D Huh? :D

Edit:

Ideally (if this function were to be used often), you'd make the Local $aHex a Global outside the function :) .

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Well it has to compare the values right. Or I have read it wrong, which I think I have :D

Yup, I did.

After testing it as a Local, it's twice as fast as yours, and about 1.5 times faster than the current one we use.

As a global, it's 3 times as fast as yours, and twice as fast as the local.

I've asked to use StringToBinary/BinaryToString method I showed here as the standard _StringToHex/_HexToString but the submission I made never made it for some reason.

But, it confirms this test is a bit faster than most with doing it this way, so if they won't submit the StringToBinary/BinaryToString maybe they'll change it to the one I just wrote.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Not sure why your using "Hex" there :D

Not sure why you're not sure.

If you're implying that StringTrimLeft would be faster, it would not. Not on my PC, converting that latin string, at least.


"be smart, drink your wine"

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  
Followers 0