Re: Hex String Handlers
Re: Hex String Handlers
- Subject: Re: Hex String Handlers
- From: Axel Luttgens <email@hidden>
- Date: Tue, 31 Dec 2002 14:21:59 +0100
Hudson Barton wrote:
> I have a little routine to convert an ascii string into its hex
> equivalent, and another to reverse the process. It is very slow on
> anything but the smallest text segment. I'm wondering if there is any
> "Terminal" or shell alternative. No osaxen please. The "hex" handler
> is below.
>
> Thanks,
>
> H.
>
> on HexString(AString)
> set thelist to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
> "A", "B", "C", "D", "E", "F"}
> set L to length of AString
> set hexvalue to ""
> repeat with i from 1 to L
> set theAscii to ASCII number of character i of AString
> set theone to round (theAscii / 16) rounding down
> set theremainder to theAscii + 1 - (theone * 16)
> try
> set Hexone to (item (theone + 1) of thelist) as string
> on error
> set Hexone to "O"
> end try
> try
> set Hextwo to (item theremainder of thelist) as string
> on error
> set Hextwo to "O"
> end try
> set theHex to Hexone & Hextwo as string
> set hexvalue to hexvalue & theHex as string
> end repeat
> set the clipboard to hexvalue
> end HexString
>
First, there's room left for speed improvement by just slightly
reorganizing your handler.
For example:
on HexString1(AString)
set thelist to "0123456789ABCDEF"
set hexvalue to ""
repeat with i in AString
set theAscii to ASCII number of i
set hexvalue to hexvalue
& character (theAscii div 16 + 1) of thelist
& character (theAscii mod 16 + 1) of thelist
end repeat
return hexvalue as string
end HexString1
(obviously, "thelist" should not be named that way anymore ;-) )
This approximately cuts execution speed by two, which is not too bad.
Depending on your needs, this may still prove insufficient.
The problem with the above script is that getting the ASCII number takes
about 70% of the execution time and the concatenations 30% (the other
statements being here negligible).
So, one could first concentrate on trying to avoid the call (or minimize
the calls) to the standard addition "ASCII number", in the line of
Arthur Knapp's proposal.
But the concatenations would remain, and I can't think at this moment of
a way to avoid them in vanilla AppleScript.
That is, you may have an estimate of a lower bound of what could be
achievable; if your handler needs a time of 100 to crunch one of your
typical strings, above variant could reduce that time to 50, and an
optimized variant to about 20-30.
So, if 100 is "very slow", it could be that 30 still is.
And since those handlers are of order n, doubling the string's length
doubles the execution time.
You asked for a shell alternative.
Here is (a very bare) one that relies on the hexdump command:
on HexString2(AString)
text 1 through -3 of (do shell script
"echo '" & AString & "' | hexdump -e '1/1 \"X\"'")
end HexString2
A call to "do shell script" is very costly too, since it means launching
a shell process, passing the command to it and retrieving its result.
But execution itself may of course prove to be blazingly fast, in
comparison to AppleScript (when asked to perform things it is not really
intended at).
So, for strings shorter than about 50 characters, your handler is faster.
But for longer strings, the "do shell script" overhead becomes extremely
worthful: handling a string of 300 characters is seemingly as fast as
handling a single character string.
HTH,
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.