Convert number to decimal
Last modification: Monday, February 15, 2010 11:12 pmConvert a number from a different base to decimal.
Required parameters
Parameter 1 (integer)The value to convert from
Parameter 2 (integer)
The base of the number to convert from
Function Calls
convertToDecimal("11111011010", 2) -- BinaryconvertToDecimal("3732", 8) -- Octal
convertToDecimal("7dA", 16) -- Hexadecimal
Implementation
on convertToDecimal(_value, _base)set _value to characters of (_value as string)
set _value to reverse of _value
set _decimalValue to 0
repeat with _i from 1 to count of _value
set _char to item _i of _value
if _char is "A" then
set _char to 10
else if _char is "B" then
set _char to 11
else if _char is "C" then
set _char to 12
else if _char is "D" then
set _char to 13
else if _char is "E" then
set _char to 14
else if _char is "F" then
set _char to 15
else
set _char to _char as integer
end if
repeat _i - 1 times
set _char to _char * _base
end repeat
set _decimalValue to _decimalValue + _char
end repeat
return _decimalValue
end convertToDecimal