Reduce binary_to_integer and back code duplication