Re: Binary => decimal conversion
Re: Binary => decimal conversion
- Subject: Re: Binary => decimal conversion
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 24 Nov 2000 18:53:47 -0800
On 11/24/00 5:16 PM, "John Welch" <email@hidden> wrote:
>
Is there an OSAX out there that does decimal to binary conversion and vice
>
versa?
>
I haven't found an osax, but I've written two handlers. They presuppose that
the binary and decimal numbers are presented as "integers" to AppleScript.
Just modify the first line of either to "string" if presented as strings.
to Binarize(dec)
if class of dec is integer then
set bin to ""
set x to dec
repeat until x < 1
set y to x mod 2
set x to x div 2
set bin to "" & y & bin
end repeat
return (bin as integer)
end if
end Binarize
to Decimalize(bin)
if class of bin is integer then
set bin to bin as string
set dec to 0
set e to 0
set l to length of bin
repeat with i from l to 1 by -1
set c to (character i of bin) as integer
set n to c * (2 ^ e) as integer
set dec to dec + n
set e to e + 1
end repeat
return dec
end if
end Decimalize
Binarize(71)
-- 1000111
Decimalize(1000111)
-- 71
--
Paul Berkowitz