AppleScript: convert a string to integer two complements

on string_2_int_twos_complement(s, base, bit_length)
	set num to string_2_int(s, base)
	if num >= 2 ^ bit_length then
		error "Can not convert string to integer. Out of range for given bit length" number 1700
	end if
	if num >= 2 ^ (bit_length - 1) then
		set num to num - 2 ^ bit_length
	end if
	return num as integer
end string_2_int_twos_complement