Creating binary from list variables?

Vlad Dumitrescu vlad_dumitrescu@REDACTED
Tue Dec 14 15:34:47 CET 2004


Hi,

One way to do it (not necessarily the best) is

 sandbox3() ->
     AsciText = "Hello Sam!",
     AsciAsBinary = list_to_binary(AsciText),
     PadLen = (15-length(AsciText))*8,
     Number = 1239,
     <<AsciAsBinary/binary, 0:PadLen, Number:32>>.

One observation is that you don't have to make everything a binary, you can send
io lists. Informally, an IO list is a deep list of characters and binaries which
can be sent to an Erlang port.

 sandbox4() ->
     AsciText = "Hello Sam!",
     PadLen = (15-length(AsciText))*8,
     Number = 1239,
     [AsciText, <<0:PadLen, Number:32>>].



regards,
Vlad



More information about the erlang-questions mailing list