Re: Hex String Handlers
Re: Hex String Handlers
- Subject: Re: Hex String Handlers
- From: Kai Edwards <email@hidden>
- Date: Sat, 28 Dec 2002 19:59:38 +0000
on Fri, 27 Dec 2002 08:57:52 -0700, Gnarlodious <email@hidden>
wrote:
>
Entity Hudson Barton spoke thus:
>
>
> 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
>
Here is what I am running, credits to Kai Edwards for the speed:
[snip: hexToRGB() handler]
Thanks for the plug, Rachel!
For Hudson's benefit, I should perhaps explain that the quoted handler was
dedicated to converting a hex colour specification (comprising 3 separate
values) to its RGB equivalent - and includes a lower-to-upper case
conversion.
I did have a look at your posted script, Hudson, to see if I could suggest
any ways that might crank up performance a bit:
=======================
>
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
=======================
By tweaking the script slightly (and combining one or two functions for
brevity), I ended up with this:
=======================
on HexString(AString)
set {HString, hexvalue} to {"0123456789ABCDEF", ""}
repeat with i in AString
tell (ASCII number i) to set {Hexone, Hextwo} to [NO BREAK]
HString's {character (it div 16 + 1), character (it mod 16 + 1)}
set hexvalue to hexvalue & Hexone & Hextwo
end repeat
set the clipboard to hexvalue
end HexString
=======================
Essentially, it's the same script, and appears to produce the same results -
However, OMM, it's generally about 3 times faster than the original.
HTH
--
Kai
_______________________________________________
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.