This script will convert a given number into a given base. Works up to base 62. I’m new to applescript - wrote this for fun. Enjoy
OS version:
property the_number : 37
property the_base : 8
set global_counter to 0
repeat
if global_counter is 0 then
set the_text to "Welcome to base-o-matic. Enter a number"
else
set the_text to "The number must be a positive integer and" & return & ¬
"the base must be an integer between 2 and 62." & return & "Enter another number."
end if
set global_counter to (global_counter + 1)
set my_number to the text returned of (display dialog the_text default answer the_number)
set my_base to the text returned of (display dialog "Enter a base." default answer the_base)
try
set my_number to my_number as integer
set my_base to my_base as integer
end try
if the class of my_number is integer and my_number > -1 and the class of ¬
my_base is integer and my_base > 1 and my_base < 63 then
set the_number to my_number
set the_base to my_base
set num_for_repeat to the_number
set num_for_repeat2 to the_number
set the_counter to 0
set char_list to every character of "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
repeat
set the_counter to (the_counter + 1)
set num_for_repeat to (num_for_repeat / the_base)
if num_for_repeat < 1 then exit repeat
end repeat
set the_num to ""
repeat with i from 1 to (the_counter)
set dec_dig to (round ((num_for_repeat2 / the_base) mod 1) * the_base)
set the_digit to item (dec_dig + 1) of char_list
set the_num to the_digit & the_num
set num_for_repeat2 to ((num_for_repeat2 - dec_dig) / the_base)
end repeat
exit repeat
end if
end repeat
display dialog ((my_number as string) & ", written in base " & my_base as string) & ", is:" & return & the_num
Okay, thanks for the very fast, efficient script.
To make everything work as intended, I modified it a little here:
removed unnecessary properties, added a property that makes sense to make a property,
removed unnecessary intermediate variables,
removed unnecessary assignments like set the_number to my_number,
removed the last exit repeat,
inserted the last display dialog into the infinite repeat loop.
removed explicit string coercions
checking for errors divided to make things clear
property aList : characters of "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
display dialog "WELCOME TO BASE-O-MATIC !" with title ¬
"CONVERTING POSITIVE INTEGER NUMBER, BASED 10" & return & ¬
"TO OTHER BASED (base ≥2 and ≤ 36)." giving up after 3
repeat
set aNumber to the text returned of (display dialog "Enter an integer number." default answer "37")
set aBase to the text returned of (display dialog "Enter an integer base ≥ 2 and ≤ 36." default answer "8")
try
set aNumber to aNumber as number
set aBase to aBase as number
on error
display notification "The number and the base must be a integer." & return & return & ¬
"The script will terminated." with title "USER INPUT ERROR" sound name "Frog"
return false
end try
if (class of aNumber ≠ integer) or (aNumber < 0) then
display notification "The number must be positive integer." & return & return & ¬
"The script will terminated." with title "USER INPUT ERROR" sound name "Frog"
return false
end if
if (class of aBase ≠ integer) or (aBase < 2) or (aBase > 36) then
display notification "The base must be integer ≥ 2 and ≤ 36." & return & ¬
return & "The script will terminated." with title "USER INPUT ERROR" sound name "Frog"
return false
end if
set {aNum, n} to {"", aNumber}
repeat
set aNum to (item (n mod aBase + 1) of my aList) & aNum
set n to n div aBase
if (n is 0) then exit repeat
end repeat
display dialog "Decimal number " & aNumber & ", written in base " & aBase & ", is: " & aNum
end repeat
The two inner repeats can be simplified still further, to just one:
set aNum to ""
set n to aNumber
repeat
set aNum to (item (n mod aBase + 1) of my aList) & aNum
set n to n div aBase
if (n is 0) then exit repeat
end repeat
The OP’s attempt to extend the number of possible bases by using lower-case letters is probably wrong, since — in hexadecimal at least — lower-case and upper-case letters are just different ways of writing the same digits.