The string representation of a number can be converted to N-48. For multi-digit numbers, you can add in binary, multiplying by the strength of the position of the digit:
-spec to_int(binary()) -> integer(). to_int(Bin) when is_binary(Bin) -> to_int(Bin, {size(Bin), 0}). to_int(_, {0, Acc}) -> erlang:trunc(Acc); to_int(<<N/integer, Tail/binary>>, {Pos, Acc}) when N >= 48, N =< 57 -> to_int(Tail, {Pos-1, Acc + ((N-48) * math:pow(10, Pos-1))}).
Performance in this case is about 100 times slower than when using the list_to_integer(binary_to_list(X)) parameter.
lafa
source share