<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Hello,</div><div><br></div><div>Here is one way to make it:</div><div><br></div><div>Head = byte_size(BinC) * 8 - 24.</div><div><<H:Head, T:24>> = BinC.<br><br>The reverse operation for binary is expensive. <br><br>Best Regards,<div>Dmitry >-|-|-(*></div><div><br></div></div><div><br>On 25.6.2014, at 4.11, Richard McLean <<a href="mailto:rmcl2303@bigpond.net.au">rmcl2303@bigpond.net.au</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div style="font-size: 14px; ">This is my first question to the mailing list so apologies if there are any newbie errors.</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">Regarding bit syntax and pattern matching, you can match the remaining part of the binary using the Rest/binary idiom as long as the element of unspecified size (ie Rest/binary) is at the end of the pattern (ie. the very last specified element)...</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">eg. suppose I have a binary of unknown size (here I'm using <<1,2,3,4>> as the example) and I want to separate it into 2 binaries - one containing the first 8 bits, and the other containing the remainder...</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">eg.</div><div style="font-size: 14px; ">1> BinA = <<1,2,3,4>>.<br><<1,2,3,4>><br>2> <<BinB:8/binary-unit:1,BinC/binary>> = BinA.<br><<1,2,3,4>><br>3> BinB.<br><<1>><br>4> BinC.<br><<2,3,4>><br><br></div><div style="font-size: 14px; ">So far so good...</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">Now suppose I want to take the same input binary (of unknown size) and this time I want to separate it into 2 binaries - one containing the LAST 24 bits (say some sort of footer), and the other containing the remainder (ie all the PRECEDING data)...</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">This should still be possible from the the compiler/erlang VM point of view as we are still specifying all the element sizes except one, but erlang generates an error if the binary element of unspecified size is anything but the last match element.</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">eg.</div><div style="font-size: 14px; ">1> BinA = <<1,2,3,4>>.<br><<1,2,3,4>><br>2> <<BinB/binary, BinC:8/binary-unit:3>> = BinA.<br>* 1: a binary field without size is only allowed at the end of a binary pattern<br><br></div><div style="font-size: 14px; ">What is the easiest way to achieve this sort of thing ?</div><div style="font-size: 14px; ">I'm supposing the binary could be reversed, the front X bits extracted, then reversing the extracted binary and the remainder, but is there an easier way ?</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">And if there are any of the erlang maintainers reading this, would it be possible in a future release to change the rules on the Rest/binary idiom to allow Rest/binary to be any element of the match pattern as long as all other elements are size specified ?</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">I don't think it would be a big change to the implement but perhaps there are hidden gotchas I'm not considering ?</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">Regards</div><div style="font-size: 14px; "><br></div><div style="font-size: 14px; ">Richard </div><div style="font-size: 14px; "><br></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>erlang-questions mailing list</span><br><span><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a></span><br><span><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a></span><br></div></blockquote></body></html>