JusGellin Posted September 2, 2008 Posted September 2, 2008 Is there a better function to break a string down to 2 Hex characters? The following works, but I would like to do it in one command if possible - just to be more efficient. Thanks <span><span class="S11">#include <string.au3> $String = "I like AutoIt3" $Hex = _StringToHex($String) $Hex = StringRegExpReplace($Hex,"(..)","\1 ") MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & $Hex) </span><span class="S8"></span></span>
Xenobiologist Posted September 2, 2008 Posted September 2, 2008 $String = "I like AutoIt3" $Hex = __StringToHex($String) MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & $Hex) Func __StringToHex($strChar) Local $aryChar, $i, $iDec, $hChar, $strHex $aryChar = StringSplit($strChar, "") For $i = 1 To $aryChar[0] $iDec = Asc($aryChar[$i]) $hChar = Hex($iDec, 2) $strHex &= $hChar & ' ' Next Return $strHex EndFunc ;==>_StringToHex Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
smashly Posted September 2, 2008 Posted September 2, 2008 $String = "I like AutoIt3" $Hex = StringRegExpReplace(StringTrimLeft(StringToBinary($String), 2),"(..)","\1 ") MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & $Hex)
JusGellin Posted September 2, 2008 Author Posted September 2, 2008 $String = "I like AutoIt3" $Hex = StringRegExpReplace(StringTrimLeft(StringToBinary($String), 2),"(..)","\1 ") MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & $Hex)Great! I knew there should be some existing functions that could be used. Thanks for your help.
Nevin Posted September 2, 2008 Posted September 2, 2008 Well technically, you're not really doing it any more efficiently if it is calling a function which is a whole bunch of lines, but it does look neater They should change my title to "Useless commentary"
Xenobiologist Posted September 2, 2008 Posted September 2, 2008 You are right! And I would bet my suggested way is faster. Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
JusGellin Posted September 2, 2008 Author Posted September 2, 2008 You are right! And I would bet my suggested way is faster.MegaThat's useful for me to know as well. I thought less code would be faster. I'm looking to process serial data coming in and breaking it down to Hex bytes. So processing speed is important. Thanks for that input.
smashly Posted September 2, 2008 Posted September 2, 2008 (edited) You are right! And I would bet my suggested way is faster. MegaI bet it's not It's actually twice as slow#include <String.au3> $Begin = TimerInit() $String = "I like AutoIt3" $Hex = _StringToHex($String) $Hex = StringRegExpReplace($Hex,"(..)","\1 ") $End = TimerDiff($begin) MsgBox(0, "JusGellin Hex", "Original String: " & $String & @LF & "Hex: " & $Hex & @LF & "Time Diff: " & $End & @LF) $Begin = TimerInit() $String = "I like AutoIt3" $Hex = StringRegExpReplace(StringTrimLeft(StringToBinary($String), 2),"(..)","\1 ") $End = TimerDiff($begin) MsgBox(0, "Smashly Hex", "Original String: " & $String & @LF & "Hex: " & $Hex & @LF & "Time Diff: " & $End & @LF) $Begin = TimerInit() $String = "I like AutoIt3" $Hex = __StringToHex($String) $End = TimerDiff($begin) MsgBox(0, "Xenobiologist Hex", "Original String: " & $String & @LF & "Hex: " & $Hex & @LF & "Time Diff: " & $End & @LF) Func __StringToHex($strChar) Local $aryChar, $i, $iDec, $hChar, $strHex $aryChar = StringSplit($strChar, "") For $i = 1 To $aryChar[0] $iDec = Asc($aryChar[$i]) $hChar = Hex($iDec, 2) $strHex &= $hChar & ' ' Next Return $strHex EndFunc ;==>_StringToHex Note to be fair you should run each example seperately, in most case by running the three example in the one go usually gives the following examples a faster time. Even so stingsplit and the for loop give a slower speed but not the slowest... Edited September 2, 2008 by smashly
JusGellin Posted September 2, 2008 Author Posted September 2, 2008 I ran the test and here's what I got: My Hex = 1.4658 sec Smashly Hex = 0.132 sec Xenobiologist Hex = 0.305 sec Both were considerably better than mine. But Smashly was the fastest Thanks for helping me evaluate this. I was wondering how I was going to do this test. Also I ran each test separately as suggested.
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