• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Binary => decimal conversion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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]


  • Prev by Date: Re: Scripting the Location Manager
  • Next by Date: Re: Binary => decimal conversion
  • Previous by thread: Re: Binary => decimal conversion
  • Next by thread: Re: Binary => decimal conversion
  • Index(es):
    • Date
    • Thread