Re: Converting binary to decimal
Re: Converting binary to decimal
- Subject: Re: Converting binary to decimal
- From: Nigel Garvey <email@hidden>
- Date: Thu, 6 Jan 2005 11:54:38 +0000
jj wrote on Wed, 05 Jan 2005 11:14:31 +0100:
[My faulty script snipped]
>Just a quick note to add a "end if" before the "end repeat":
Thanks for pointing that out, jj. Another late-night posting, I'm afraid.
:-\
>* Do you keep record of your handlers, or you just remember and write them
>from scratch when you need 'em? ;-)
A bit of both. :-) I made the wrong decision in this case, as I've just
found a faster one in my collection:
on binToNum from bin given sign:sign
if (bin's class is not in {string, Unicode text}) then ¬
error "binToNum: Input is not text."
set binLength to (count bin)
considering case
if (sign) and (bin begins with "1") then
set n to -(2 ^ (binLength mod 8 - 8))
else
set n to 0
end if
try
set i to 1
set j to (binLength - 1) mod 8 + 1
repeat until (i > binLength)
set b to text i thru j of bin
set int to b as integer
if (int < 0) or (b > "11111111") then error
set n to n * 256 + int div 10000000 * 128 ¬
+ int div 1000000 mod 10 * 64 ¬
+ int div 100000 mod 10 * 32 ¬
+ int div 10000 mod 10 * 16 ¬
+ int div 1000 mod 10 * 8 ¬
+ int div 100 mod 10 * 4 ¬
+ int div 10 mod 10 * 2 ¬
+ int mod 10 as integer
set i to j + 1
set j to i + 7
end repeat
on error
error "binToNum: Non-binary character(s) in input."
end try
end considering
return n
end binToNum
binToNum2 from "111111111111" without sign
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