Re: Converting Hex to binary
Re: Converting Hex to binary
- Subject: Re: Converting Hex to binary
- From: Nigel Garvey <email@hidden>
- Date: Sun, 9 Jan 2005 15:02:14 +0000
Bernardo Hoehl wrote on Sat, 8 Jan 2005 17:40:17 -0200:
>set theInput to "0309"
>
>set HexList to every character of ("0123456789ABCDEF")
>
>set binList to {"0000", "0001", "0010", "0011", "0100", "0101", "0110",
>"0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}
>set ConvertedBin to ""
>
>repeat with i from 1 to the count of theInput
> repeat with ii from 1 to the count of HexList
> if item i of theInput is item ii of HexList then
> set ConvertedBin to ConvertedBin & item ii of binList
> end if
> end repeat
>end repeat
>return ConvertedBin --returns "0000001100001001"
>
>
>Please:
>
>Could you comment my script?
Hi, Bernardo. As a rule, it's better not to use nested repeats if there's
some other sensible way of achieving what you want. Even here, you could
speed things up by counting HexList before entering the repeats (so that
you don't do it every time) and also by putting an 'exit repeat' in the
'if' block in the inner loop (to save carrying on to the end of HexList
when you've already found what you want).
set HexListLength to the count of HexList
repeat with i from 1 to the count of theInput
repeat with ii from 1 to HexListLength
if item i of theInput is item ii of HexList then
set ConvertedBin to ConvertedBin & item ii of binList
exit repeat
end if
end repeat
end repeat
A faster way to do the conversion is to use text item delimiters:
set theInput to "0309"
set HexString to "x0123456789ABCDEF"
set binList to {"0000", "0001", "0010", "0011", "0100", "0101", "0110",
"0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}
set ConvertedBin to {}
set astid to AppleScript's text item delimiters
repeat with i from 1 to (count theInput)
set AppleScript's text item delimiters to character i of theInput
-- Count the characters before this one in HexString.
set c to (count HexString's first text item)
set end of ConvertedBin to item c of binList
end repeat
set AppleScript's text item delimiters to ""
set ConvertedBin to ConvertedBin as string
set AppleScript's text item delimiters to astid
return ConvertedBin
NG
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden