<div dir="ltr">Since no-one else mentioned it:<div>What you're looking for is probably the "unit" modifier:</div><div><div>1> Num = 123.</div><div>123</div><div>2> <<Num:4/unit:8>>.</div><div><<0,0,0,123>></div></div><div><br></div><div>The default unit for (integers/floats/bitstring) is 1 bit, while the default unit for binaries ('bytes') is 1 byte.</div><div><br></div><div>/Erik</div></div><div class="gmail_extra"><br><div class="gmail_quote">2014-12-13 15:35 GMT+01:00 PAILLEAU Eric <span dir="ltr"><<a href="mailto:eric.pailleau@wanadoo.fr" target="_blank">eric.pailleau@wanadoo.fr</a>></span>:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">5> A = <<1,2,8>>.<br>
<<1,2,8>><br>
6> P = <<A:2/binary>>.<br>
<<1,2>><br>
<br>
Got it ?<br>
<br>
Le 13/12/2014 15:32, PAILLEAU Eric a Ã©crit :<div class="HOEnZb"><div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
1> Num = 128.<br>
128<br>
2> BNum = <<Num>>.<br>
<<128>><br>
3>  Packet = <<BNum:1/binary>>.<br>
<<128>><br>
<br>
regards.<br>
<br>
<br>
Le 13/12/2014 15:25, Santosh Kumar a Ã©crit :<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Wes,<br>
I just tried <<BNum:4/binary>> and that didn't work as well. Here are<br>
the complete steps I ran through in erl:<br>
<br>
1> Num = 128.<br>
<br>
128<br>
<br>
2> BNum = <<Num>>.<br>
<br>
<<128>><br>
<br>
3> Packet = <<BNum:4/binary>>.<br>
<br>
** exception error: bad argument<br>
<br>
<br>
Any help much appreciated.<br>
<br>
<br>
Thanks<br>
<br>
Santosh<br>
<br>
<br>
On Fri, Dec 12, 2014 at 11:54 PM, Wes James <<a href="mailto:comptekki@gmail.com" target="_blank">comptekki@gmail.com</a><br>
<mailto:<a href="mailto:comptekki@gmail.com" target="_blank">comptekki@gmail.com</a>>> wrote:<br>
<br>
  Â  According to:<br>
<br>
  Â  <a href="http://www.erlang.org/doc/programming_examples/bit_syntax.html" target="_blank">http://www.erlang.org/doc/<u></u>programming_examples/bit_<u></u>syntax.html</a><br>
<br>
  Â  after the / you need integer, float or binary, i.e.,<br>
<<BNum:4/binary>><br>
<br>
  Â  -wes<br>
<br>
<br>
  Â  On Fri Dec 12 2014 at 9:22:59 PM Santosh Kumar <<a href="mailto:santosh79@gmail.com" target="_blank">santosh79@gmail.com</a><br>
  Â  <mailto:<a href="mailto:santosh79@gmail.com" target="_blank">santosh79@gmail.com</a>>> wrote:<br>
<br>
  Â  Â  Â  Hrm, I'm not sure I understand. Here's some code:<br>
<br>
  Â  Â  Â  Num = 128.<br>
<br>
  Â  Â  Â  128<br>
<br>
<br>
  Â  Â  Â  So, Num is a number. I then do:<br>
<br>
  Â  Â  Â  BNum = <<Num>>.<br>
<br>
<br>
  Â  Â  Â  Now, BNum is a binary. I check it too:<br>
<br>
  Â  Â  Â  is_binary(BNum).<br>
<br>
  Â  Â  Â  true<br>
<br>
<br>
  Â  Â  Â  I now, try doing:<br>
<br>
  Â  Â  Â  Packet = <<BNum:4/bytes>>.<br>
<br>
  Â  Â  Â  ** exception error: bad argument<br>
<br>
<br>
  Â  Â  Â  Here, I am trying to create another binary Packet, that is 4<br>
  Â  Â  Â  bytes long as and has as it's contents BNum. If what you say is<br>
  Â  Â  Â  true and "/bytes" is used to pack Binary data this would work.<br>
  Â  Â  Â  Unless I am missing something.<br>
<br>
<br>
  Â  Â  Â  Thanks for your response.<br>
<br>
<br>
  Â  Â  Â  Santosh<br>
<br>
<br>
  Â  Â  Â  On Fri, Dec 12, 2014 at 10:39 PM, Bob Ippolito <<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a><br>
  Â  Â  Â  <mailto:<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>>> wrote:<br>
<br>
  Â  Â  Â  Â  Â  'bytes' is for packing binaries, not numbers. The<br>
  Â  Â  Â  Â  Â  working example you have is the correct way to do it.<br>
<br>
<br>
  Â  Â  Â  Â  Â  On Friday, December 12, 2014, Santosh Kumar<br>
  Â  Â  Â  Â  Â  <<a href="mailto:santosh79@gmail.com" target="_blank">santosh79@gmail.com</a> <mailto:<a href="mailto:santosh79@gmail.com" target="_blank">santosh79@gmail.com</a>>> wrote:<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  Hi group,<br>
  Â  Â  Â  Â  Â  Â  Â  I was trying to pack a number into 4 bytes and was<br>
  Â  Â  Â  Â  Â  Â  Â  having a hard time doing it. Here is what I was trying:<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  102> Num = 128.<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  128<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  103> <<Num:4/bytes>>.<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  ** exception error: bad argument<br>
<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  While this works:<br>
<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  104> <<Num:32>>.<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  <<0,0,0,128>><br>
<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  Here the packing into 32 bits works but the packing into<br>
  Â  Â  Â  Â  Â  Â  Â  4 bytes fails. Not sure why.<br>
<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  Any help, much appreciated.<br>
<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  Thanks<br>
<br>
  Â  Â  Â  Â  Â  Â  Â  Santosh<br>
<br>
  Â  Â  Â  ______________________________<u></u>___________________<br>
  Â  Â  Â  erlang-questions mailing list<br>
  Â  Â  Â  <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@<u></u>erlang.org</a>><br>
  Â  Â  Â  <a href="http://erlang.org/mailman/__listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/__<u></u>listinfo/erlang-questions</a><br>
  Â  Â  Â  <<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a>><br>
<br>
<br>
<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
<br>
</blockquote>
<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
<br>
</blockquote>
<br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</div></div></blockquote></div></div>