[erlang-questions] direct binary to integer conversion

Steve Davis steven.charles.davis@REDACTED
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 <steven.charles.da...@REDACTED>
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 <muha...@REDACTED> 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
>
> > --
> > Muharem Hrnjadovic <muha...@REDACTED>
> > Public key id : B2BBFCFC
> > Key fingerprint : A5A3 CC67 2B87 D641 103F 5602 219F 6B60 B2BB FCFC
>
> >  signature.asc
> > < 1KViewDownload
>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questi...@REDACTED://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questi...@REDACTED://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list