integer to binary
Fri Oct 7 13:45:55 CEST 2005
It is actually easier not to use the bit syntax.
For positive numbers the following function will do the trick:
int_to_bin(0, [B|Acc]) when B < 256 ->
int_to_bin(N, Acc) ->
int_to_bin(N bsr 8, [N band 16#ff|Acc]).
chandru <chandrashekhar.mullaparthi@REDACTED> writes:
> I want to convert an integer to a binary without having to
> guess/figure-out how many bytes are required to fit that integer.
> There doesn't seem to be a way to do this with the bit syntax.
> 11> <<12345678901:40>>.
> 12> <<12345678901:48>>.
> In the example above, the integer will fit in 5 bytes but I can't
> think of a way to specify this without trial and error. Any
> suggestions? I can write a function which will figure out how many
> bytes are required but it'll be nice to have a built-in way of doing
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions