# [erlang-questions] direct binary to integer conversion

Steve Davis <>
Sun Jan 1 05:07:41 CET 2012

```er...

parse_integer(<<\$-, Bin/binary>>) ->
-1 * parse_integer(Bin, 0);parse_integer(Bin) ->
parse_integer(Bin, 0).
parse_integer(<<X, Bin/binary>>, Acc) when X >= \$0 andalso X =< \$9 ->
Acc0 = Acc * 10 + (X - \$0),
parse_integer(Bin, Acc0);parse_integer(<<>>, Acc) ->
Acc.
On Dec 31, 10:02 pm, Steve Davis <>
wrote:
> Maybe something like...
>
> %%parse_integer(<<\$-, Bin/binary>>) ->   -1 * parse_integer(Bin,
> 0);parse_integer(Bin) ->     parse_integer(Bin, 0).%parse_integer(<<X, Bin/
> binary>>, Acc) when X >= \$0 andalso X =< \$9 ->   Acc0 = Acc * 10 + (X -
> \$0),    parse_integer(Bin, Acc0);parse_integer(<<>>, Acc) ->     Acc.
> On Dec 31, 8:38 am, Muharem Hrnjadovic <> wrote:
>
>
>
>
>
>
>
> > Hello there!
>
> > Is there a way to convert a binary to an integer *directly*? Right now I
> > am using the following construct:
>
> >     string:to_integer(binary_to_list(B))
>
> > where B is a binary.
>
> > Best regards/Mit freundlichen Grüßen
>
> > --
> > Public key id : B2BBFCFC
> > Key fingerprint : A5A3 CC67 2B87 D641 103F 5602 219F 6B60 B2BB FCFC
>
> >  signature.asc