Parsing user name and password from binary in Erlang + Cowboy
Your approach is necessary because of the need to extract the length of the Username field before using it to extract the Username field itself. However, you needn't use bitstring anywhere because you're only sending whole bytes, not bitfields. You can change all occurrences of bitstring to binary instead, and with that change you also no longer need to specify the length of Username in bits, so you no longer need LU2 and can just use LengthUser directly:

-define(LOGIN, 1).

websocket_handle({binary, &lt;&lt;?LOGIN:8,LengthUser:8, Rest/binary&gt;&gt;}, Req, State) -&gt;
    &lt;&lt;Username:LengthUser/binary, Password/binary&gt;&gt; = Rest,
    %% Do the actual logging in...


And finally, note that by convention, Erlang comments on a line by themselves at the current indentation level start with %%, not %. Single % should be used only for end-of-line comments. Not sure about other editors but the emacs erlang-mode indents comments this way, so if an emacs user worked on your code your comments might get wildly indented if the emacs developer auto-indented the buffer and didn't check everything afterwards.