Decimal to Binay Converter

Hi MacScripter, i have in mind a new easy script, but i don’t know how do it.

How the Subject suggests, this time i would scripting a converter from decimals to binary.

That’s a problem… How can i do?? o_O

global a
display dialog "Inserisci il numero da convertire" default answer ""
set a to text returned of result
--converting process
display dialog "il risultato è " & rest buttons {"OK"}

Hi,

simple solution


set a to decToBin(10) --> 1010

on decToBin(decValue)
	set binValue to ""
	repeat while decValue > 0
		set binValue to ((decValue mod 2) as text) & binValue
		set decValue to decValue div 2
	end repeat
	return binValue
end decToBin


thank you much friend, but i would like to insert from keyboard the number… and if i give a letter, the system show an error display

prego


display dialog "Inserisci il numero da convertire" default answer ""
try
	set numeroInserito to (text returned of result) as integer
	
	--converting process
	display dialog "il risultato è " & decToBin(numeroInserito) buttons {"OK"}
on error
	display dialog "Per favore inserisca un numero !" buttons {"OK"}
end try

on decToBin(decValue)
	set binValue to ""
	repeat while decValue > 0
		set binValue to ((decValue mod 2) as text) & binValue
		set decValue to decValue div 2
	end repeat
	return binValue
end decToBin

:o

Master… :o

I upgrade my little app… Thank you much

my startApp()
 
on startApp()
	display dialog "Inserisci il numero da convertire" default answer "" with title "Convertitore Decimale binario" default button 2 --with icon file "Macintosh HD:Users:Light:Desktop:Convertitore Decimale binario.app:Contents:Resources:Convertitore Decimale binario.icns"
	try
		set numeroInserito to (text returned of result) as integer
		display dialog "il risultato è " & decToBin(numeroInserito) buttons {"OK"} with title "Convertitore Decimale binario" default button 1 --with icon file "Macintosh HD:Users:Light:Desktop:Convertitore Decimale binario.app:Contents:Resources:Convertitore Decimale binario.icns"
	on error
		display dialog "Errore di sintassi" buttons {"Annulla", "Ripeti"} with icon stop with title "Convertitore Decimale binario" default button 2
		if button returned of the result = "Ripeti" then
			my startApp()
		end if
	end try
end startApp
 
on decToBin(decValue)
	set binValue to ""
	repeat while decValue > 0
		set binValue to ((decValue mod 2) as text) & binValue
		set decValue to decValue div 2
	end repeat
	return binValue
end decToBin