Two handlers that converts integers to binary strings and binary strings to integers without xtras.
It came up when I instead of a progressbar wanted to show progress with a binary string.
The final project used a standard progressbar:-)
/Mango
--put intToBin(65,0) returns a binary string without zeropadding
-- "1000001"
--errorcodes
-- -1 = integer value > max possible value with wanted number of digits
-- -2 = integer value < 0
-- -3 = integer not an integer
-- -4 = binbase not an integer
on intToBin int, binbase
--errorchecking
if integerp(binbase) = 0 then return -4
if integerp(int) = 0 then return -3
if int < 0 then return -2
if binbase > 0 and integer(power(2,binbase)) > int = 0 then return -1
--end errorchecking
--calculate automatic binbase if binbase = 0
if binbase = 0 then
tint = int
binbase = 1
repeat while tint > 1
tint = tint / 2
binbase = binbase +1
end repeat
end if
--end automatic binbase calculation
repeat with i = binbase down to 1
base = integer(power(2,i-1))
a = int / base
int = int - (a*base)
s = s & a
end repeat
return s
end
--usage----
--put binToInt("1000001")
-- 65
--errorcodes
-- -1 = string contains digits other than 0 and 1
-- -2 = string is not a string
on binToInt abin
if stringp(abin) = 0 then return -2
tmpr = 1
tint = 0
repeat with i = 1 to abin.char.count
b = abin.char[abin.char.count -i +1]
case b of
"1":
tint = tint + tmpr
"0":
otherwise
return -1
end case
tmpr = tmpr *2
end repeat
return tint
end
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA