Bit syntax endianness confusion

James Hague jamesh@REDACTED
Tue Dec 5 18:50:34 CET 2000


Tony Rogvall wrote:

>Expression like yours above does not make much
>sense (Field1:4/little) since only byte sequences
>are affected by the endian order.

Yes, I suspected as much.  Since I'm dealing with a file of N fields of
fixed size (specifically 64 bits), it is easy to do a swizzle up front with
this code:

byteswap(B) ->
   list_to_binary(lists:reverse(binary_to_list(B))).

preprocess(B) -> preprocess(B, []).
preprocess(<<B:8/binary, Rest/binary>>, Acc) ->
   preprocess(Rest, [byteswap(B)|Acc]);
preprocess(<<>>, Acc) ->
   list_to_binary(lists:reverse(Acc)).

Using binary patterns directly in byteswap is faster for the case when the
input binary is 4 bytes, but I didn't test the 8 byte case.  Either version
is plenty fast for the disassembler I'm working on.

It would be nice to be able to specify an overall endianess for a pattern,
maybe like this:

<<A:8,B:8,C:8/D:8>>/little

James




More information about the erlang-questions mailing list