Creating binary from list variables?

Ericsson, Bjorn Bjorn.Ericsson@REDACTED
Tue Dec 14 14:44:58 CET 2004

Hello, here is another newbie question. I have read chapter 6.16 of the
Reference Manual, but not succeded to solve this problem.

How do I create a binary, to send with gen_tcp:send, from variables of
different types and dynamic length, when I want the final binary to be of
fixed length? (As it usually is defined in socket interfaces)

I keep getting "badarg" when I try different approaches for lists:

sandbox1() ->
    AsciText = "Hello there!",
    Number = 123,
    Binary = <<AsciText:120, Number:32>>,
sandbox2() ->
    AsciText = "Hello Sam!",
    AsciAsBinary = list_to_binary(AsciText),
    Number = 1239,
    Binary = <<AsciAsBinary:120, Number:32>>,



More information about the erlang-questions mailing list