[erlang-questions] binary to bigint?

Philip Robinson <>
Tue Aug 7 14:06:42 CEST 2007


One possibility...

-module(test).
-export([foldl_bin/4]).

foldl_bin(_Fun, Acc, <<>>, _BytesElem) -> Acc;
foldl_bin(Fun, Acc, Bin, BytesElem) ->
    {Elem,Rest} = split_binary(Bin, BytesElem),
    foldl_bin(Fun, Fun(Elem, Acc), Rest, BytesElem).

1> c(test), test:foldl_bin(fun(<<I>>, Acc) -> Acc * 256 + I end, 0,
crypto:rand_bytes(40), 1).

This makes a few assumptions regarding the integer interpretation of
that 40-byte binary.

Cheers,
Philip


On 8/7/07, Jim McCoy <> wrote:
> After bouncing through the various bits of doc related to io_lib I
> can't quite seem to put together a quick function that will convert an
> arbitrary binary to a big int (e.g. I want the integer for
> crypto:rand_bytes(40)...)
>
> Any hints?
>
> jim
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list