02.12.2012 21:12:00 • Categories: Applescript • Tags: Applescript
AppleScript: convert integer to string two complement
on int_2_string_twos_complement(i, base, bit_length)
-- check for range
set i to i as integer
if i >= 2 ^ (bit_length - 1) then
error "Can not convert integer to string integer too high for given bit length" number 1700
end if
if i < -(2 ^ (bit_length - 1)) then
error "Can not convert integer to string integer too low for given bit length" number 1700
end if
if i < 0 then
set i to i + 2 ^ bit_length
end if
set res to int_2_string(i, base)
-- filling with zeroes
-- calculate number of digitsset
set dc to (length of int_2_string(2 ^ bit_length - 1, base))
repeat while length of res < dc
set res to "0" & res
end repeat
return res
end int_2_string_twos_complement