Jump to content
Sign in to follow this  
JusGellin

String to Hex

Recommended Posts

JusGellin

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>

Share this post


Link to post
Share on other sites
Xenobiologist

$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

Share this post


Link to post
Share on other sites
Nevin

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 :D

They should change my title to "Useless commentary" ;)

Share this post


Link to post
Share on other sites
Xenobiologist

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

Share this post


Link to post
Share on other sites
JusGellin

You are right! ;) And I would bet my suggested way is faster.

Mega

That'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.

Share this post


Link to post
Share on other sites
smashly

You are right! :D And I would bet my suggested way is faster.

Mega

I 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 by smashly

Share this post


Link to post
Share on other sites
JusGellin

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.

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  

×