Re: Converting binary to decimal
Re: Converting binary to decimal
- Subject: Re: Converting binary to decimal
- From: jj <email@hidden>
- Date: Wed, 05 Jan 2005 11:14:31 +0100
> Michelle Steiner wrote on Mon, 3 Jan 2005 22:59:43 -0700:
>
>> On Jan 3, 2005, at 8:59 PM, Bernardo Hoehl wrote:
>>
>>> Has anyone ever written a handler to convert binary to decimal?
>>
>> Here is one that uses plain vanilla Applescript; it's slower that
>> calling a shell script, though.
>
> Not on my machine (G3 400 PowerBook, Jaguar, 12-digit input). 500
> iterations of the conversion part of your code take 0.6 seconds (Joshua's
> about the same), whereas 500 iterations of Gnarlodious's shell script
> take 15 seconds. jj's handler covers the same ground in about 0.8
> seconds, and this in 0.4 seconds:
>
> on binToNum from bin given sign:sign
> try
> script o
> property binChars : bin's characters
> end script
> on error
> error "binToNum: Input is not a string."
> end try
> set num to 0
> if (sign) then set num to -1
> considering case
> repeat with i from 1 to (count bin)
> set b to item i of o's binChars
> if (b is in "10") then
> set num to num * 2 + b
> else
> error "binToNum: Invalid character(s) in input string."
> end repeat
> end considering
> return num
> end binToNum
>
> binToNum from "111111111111" without sign
>
>
> NG
Just a quick note to add a "end if" before the "end repeat":
on binToNum from bin given sign:sign
try
script o
property binChars : bin's characters
end script
on error
error "binToNum: Input is not a string."
end try
set num to 0
if (sign) then set num to -1
considering case
repeat with i from 1 to (count bin)
set b to item i of o's binChars
if (b is in "10") then
set num to num * 2 + b
else
error "binToNum: Invalid character(s) in input string."
end if
end repeat
end considering
return num
end binToNum
binToNum from "111111111111" without sign
jj
* Do you keep record of your handlers, or you just remember and write them
from scratch when you need 'em? ;-)
--
http://www.macscripter.net/
http://www.osaxen.com/
_______________________________________________
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