construct binary

Matthias Lang <>
Thu Jul 7 08:54:43 CEST 2005


Here's my try:

decode(<<N_channels:5, Alignment_bits:3, Tail/binary>>) ->
        decode2(N_channels, <<Alignment_bits:3, Tail/binary, 0:5>>).
decode2(0, _) ->
        [];
decode2(N, <<C:11, A:5, T/binary>>) ->
        [C|decode2(N-1, <<A:5, T/binary, 0:3>>)].

encode(Channels) when length(Channels) < 32 ->
        encode2(<<(length(Channels)):5, 0:3>>, 5, 3, Channels).
encode2(Bin, _Used, _Pad, []) ->
        Bin;
encode2(Bin, Bits, Pad, [C|Cs]) when C >= 0, C < 2048 ->
        <<Head:Bits, _:Pad>> = Bin,
        New_pad = (Pad - 11) band 7,
        encode2(<<Head:Bits, C:11, 0:New_pad>>, Bits + 11, New_pad, Cs).

Matthias

--------------------

Vance Shipley writes:
 > Stephen,
 > 
 > I think this does it.
 > 
 > 	-Vance
 > 
 > 
 > 1> prl:decode(<<2:5, 1:11, 2:11, 0:5>>).
 > [1,2]
 > 
 > 
 > -module(prl).
 > -export([decode/1]).
 > 
 > decode(<<NumChans:5, _Pad:3, _Rest/binary>> = Bin) ->
 >    decode(Bin, NumChans, NumChans, []).
 > decode(_, _, 0, Acc) ->
 >    Acc;
 > decode(Bin, NumChans, N, Acc) ->
 >    SkipBefore = (N - 1) * 11,
 >    SkipAfter = (NumChans - N) * 11,
 >    Pad = 8 - ((NumChans * 11 + 5) rem 8),
 >    <<_:5, _:SkipBefore, Chan:11, _:SkipAfter, _:Pad>> = Bin,
 >    decode(Bin, NumChans, N - 1, [Chan | Acc]).
 > 
 > 
 > On Wed, Jul 06, 2005 at 04:59:47PM -0700, Stephen Han wrote:
 > }  
 > }  I am trying to write a IS-683 PRL encode/decode tool for our testing.



More information about the erlang-questions mailing list