[erlang-questions] Re. How to decode octet to septets using erlang (prasantha kumara)

Hynek Vychodil <>
Thu Mar 5 17:51:58 CET 2009


Code below does more than 20MB/s (more than 14 times faster than yours) on
my 2.2GHz notebook using native:

unpack3(UD) -> unpack3(<<>>, UD, <<>>).

unpack3(_, <<>>, Acc) -> Acc;    % <-- implement your padding here if you
want
unpack3(<<>>, <<A1:1, B1:7, A2:2, B2:6, A3:3, B3:5, A4:4, B4:4, A5:5, B5:3,
A6:6, B6:2, A7:7, B7:1, Rest/bytes>>, Acc) ->
unpack3(<<>>, Rest, <<Acc/bytes, 0:1, B1:7, 0:1, B2:6, A1:1, 0:1, B3:5,
A2:2, 0:1, B4:4, A3:3, 0:1, B5:3, A4:4, 0:1, B6:2, A5:5, 0:1, B7:1, A6:6,
0:1, A7:7>>);
unpack3(<<>>, <<A:1, B:7, Rest/bytes>>, Acc) -> unpack3(<<A:1>>, Rest,
<<Acc/bytes, 0:1, B:7>>);
unpack3(<<P:1>>, <<A:2, B:6, Rest/bytes>>, Acc) -> unpack3(<<A:2>>, Rest,
<<Acc/bytes, 0:1, B:6, P:1>>);
unpack3(<<P:2>>, <<A:3, B:5, Rest/bytes>>, Acc) -> unpack3(<<A:3>>, Rest,
<<Acc/bytes, 0:1, B:5, P:2>>);
unpack3(<<P:3>>, <<A:4, B:4, Rest/bytes>>, Acc) -> unpack3(<<A:4>>, Rest,
<<Acc/bytes, 0:1, B:4, P:3>>);
unpack3(<<P:4>>, <<A:5, B:3, Rest/bytes>>, Acc) -> unpack3(<<A:5>>, Rest,
<<Acc/bytes, 0:1, B:3, P:4>>);
unpack3(<<P:5>>, <<A:6, B:2, Rest/bytes>>, Acc) -> unpack3(<<A:6>>, Rest,
<<Acc/bytes, 0:1, B:2, P:5>>);
unpack3(<<P:6>>, <<A:7, B:1, Rest/bytes>>, Acc) -> unpack3(<<>>, Rest,
<<Acc/bytes, 0:1, B:1, P:6, 0:1, A:7>>).


On Thu, Mar 5, 2009 at 4:44 PM, Hynek Vychodil <>wrote:

> Well, GSM using weird coding.
> This similar to your solution is:
>
> unpack(UD) ->
>     << <<0:1,A:1,B:1,C:1,D:1,E:1,F:1,G:1>> ||
>         <<G:1,F:1,E:1,D:1,C:1,B:1,A:1>> <=
>             << <<A:1,B:1,C:1,D:1,E:1,F:1,G:1,H:1>> ||
> <<H:1,G:1,F:1,E:1,D:1,C:1,B:1,A:1>> <= UD >>
>     >>.
>
> It is 2-10 times faster on my laptop (depend of GC hit) in byte code and 4
> times in native.
>
>
> 2009/3/5 Tony Seebregts <>
>
>> Hmmm , the short answer is that the code I suggested is taken from a
>> module to convert a GSM octet stream into a septet stream and it gives a
>> different answer when using your more compact code :-) :
>> UD:      << 203,176,184,204,86,191,1 >>
>> Mine:    [75,97,98,101,108,106,111,0]
>> Yours:  << 101,108,23,12,98,90,126,1>>
>>
>> I remember the GSM octet-septet conversion being quite awkward to get
>> right because the bytes were the 'wrong way round'  which is probably why I
>> ended up with the code I did i.e. having to use flip(...).
>>
>>
>> 2009/3/5 Hynek Vychodil <>
>>
>>> Why not?
>>>
>>>
>>> unpack(UD) ->
>>>   << <<0:1,X/bits>> || <<X:7/bits>> <= UD >>.
>>>
>>> 2009/3/5 Tony Seebregts <>
>>>
>>>> This is an extract from the code I use:
>>>>
>>>>> unpack(UD) ->
>>>>>    septets(list_to_binary([ flip(X) || X <- binary_to_list(UD) ])).
>>>>>
>>>>> septets(T) ->
>>>>>     [ flipx(X) || <<X:7>> <= T].
>>>>>
>>>>> % Flips the bits in an octet/septet for easy unpacking
>>>>>
>>>>> flip(Octet) ->
>>>>>    <<B8:1,B7:1,B6:1,B5:1,B4:1,B3:1,B2:1,B1:1>>  = <<Octet>>,
>>>>>    <<B1:1,B2:1,B3:1,B4:1,B5:1,B6:1,B7:1,B8:1>>.
>>>>>
>>>>
>>>> There is probably a better way of doing it but I was in a rush at the
>>>> time :-)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> 
>>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>
>>>
>>>
>>> --
>>> --Hynek (Pichi) Vychodil
>>>
>>> Analyze your data in minutes. Share your insights instantly. Thrill your
>>> boss.  Be a data hero!
>>> Try Good Data now for free: www.gooddata.com
>>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>
>
>
> --
> --Hynek (Pichi) Vychodil
>
> Analyze your data in minutes. Share your insights instantly. Thrill your
> boss.  Be a data hero!
> Try Good Data now for free: www.gooddata.com
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill your
boss.  Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090305/00520a4a/attachment.html>


More information about the erlang-questions mailing list