Re: Binary => decimal conversion
Re: Binary => decimal conversion
- Subject: Re: Binary => decimal conversion
- From: "Marc K. Myers" <email@hidden>
- Date: Sat, 25 Nov 2000 01:17:32 -0500
- Organization: [very little]
John Welch wrote:
>
Date: Fri, 24 Nov 2000 20:16:06 -0500
>
Subject: Binary => decimal conversion
>
From: John Welch <email@hidden>
>
To: "AppleScript User's List" <email@hidden>
>
>
Is there an OSAX out there that does decimal to binary conversion and vice
>
versa?
I don't know of an osax that does this, but the mechanics of the
conversion are fairly straightforward. AppleScript does arithmetic with
blazing speed. This is a script I put together as an exercise when I
was first playing around with it.
property theDigits : "0123456789"
set theInt to ""
repeat until class of theInt is integer
set theText to text returned of (display dialog "Enter a decimal
number:" default answer "0")
set textOK to true
repeat with i from 1 to length of theText
if character i of theText is not in theDigits then
display dialog "\"" & theText & "\" could not be converted
to an integer"
set textOK to false
end if
end repeat
if textOK then
try
set theInt to theText as integer
on error
display dialog "\"" & theText & "\" could not be converted
to an integer"
end try
end if
end repeat
if theInt < 0 then
display dialog "This converter only works on positive integers"
else if theInt = 0 then
display dialog "The binary representation of \"0\" is \"0\""
else
set binString to ""
set the exPo to 28
repeat while exPo > -1
if theInt div (2 ^ exPo) = 1 then
set binString to binString & "1"
set theInt to theInt - (2 ^ exPo)
else
if length of binString > 0 then
set binString to binString & "0"
end if
end if
set exPo to exPo - 1
end repeat
display dialog "The binary representation of \"" & theText & ,
"\" is \"" & binString & "\""
end if
Marc [11/25/00 1:17:17 AM]