[erlang-questions] Bit Packing question

PAILLEAU Eric <>
Sat Dec 13 15:32:50 CET 2014


1> Num = 128.
128
2> BNum = <<Num>>.
<<128>>
3>  Packet = <<BNum:1/binary>>.
<<128>>

regards.


Le 13/12/2014 15:25, Santosh Kumar a écrit :
> Hi Wes,
> I just tried <<BNum:4/binary>> and that didn't work as well. Here are
> the complete steps I ran through in erl:
>
> 1> Num = 128.
>
> 128
>
> 2> BNum = <<Num>>.
>
> <<128>>
>
> 3> Packet = <<BNum:4/binary>>.
>
> ** exception error: bad argument
>
>
> Any help much appreciated.
>
>
> Thanks
>
> Santosh
>
>
> On Fri, Dec 12, 2014 at 11:54 PM, Wes James <
> <mailto:>> wrote:
>
>     According to:
>
>     http://www.erlang.org/doc/programming_examples/bit_syntax.html
>
>     after the / you need integer, float or binary, i.e., <<BNum:4/binary>>
>
>     -wes
>
>
>     On Fri Dec 12 2014 at 9:22:59 PM Santosh Kumar <
>     <mailto:>> wrote:
>
>         Hrm, I'm not sure I understand. Here's some code:
>
>         Num = 128.
>
>         128
>
>
>         So, Num is a number. I then do:
>
>         BNum = <<Num>>.
>
>
>         Now, BNum is a binary. I check it too:
>
>         is_binary(BNum).
>
>         true
>
>
>         I now, try doing:
>
>         Packet = <<BNum:4/bytes>>.
>
>         ** exception error: bad argument
>
>
>         Here, I am trying to create another binary Packet, that is 4
>         bytes long as and has as it's contents BNum. If what you say is
>         true and "/bytes" is used to pack Binary data this would work.
>         Unless I am missing something.
>
>
>         Thanks for your response.
>
>
>         Santosh
>
>
>         On Fri, Dec 12, 2014 at 10:39 PM, Bob Ippolito <
>         <mailto:>> wrote:
>
>             'bytes' is for packing binaries, not numbers. The
>             working example you have is the correct way to do it.
>
>
>             On Friday, December 12, 2014, Santosh Kumar
>             < <mailto:>> wrote:
>
>                 Hi group,
>                 I was trying to pack a number into 4 bytes and was
>                 having a hard time doing it. Here is what I was trying:
>
>                 102> Num = 128.
>
>                 128
>
>                 103> <<Num:4/bytes>>.
>
>                 ** exception error: bad argument
>
>
>                 While this works:
>
>
>                 104> <<Num:32>>.
>
>                 <<0,0,0,128>>
>
>
>                 Here the packing into 32 bits works but the packing into
>                 4 bytes fails. Not sure why.
>
>
>                 Any help, much appreciated.
>
>
>                 Thanks
>
>                 Santosh
>
>         _________________________________________________
>         erlang-questions mailing list
>          <mailto:>
>         http://erlang.org/mailman/__listinfo/erlang-questions
>         <http://erlang.org/mailman/listinfo/erlang-questions>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list