Jump to content

String to Hex


 Share

Recommended Posts

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>
Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...