How to convert a binary to a list of 2 byte ints

Ivan Uemlianin ivan@REDACTED
Thu May 27 11:31:21 CEST 2010


Dear All

binary_to_list/1 converts a binary to a list of integers, mapping each 
byte onto an integer.  The data I have, I need to map each pair of bytes 
onto an integer (signed, little-endian).

e.g.:

     1> b2i(<<18,255,39,255,73,255,90,255,80,255>>).

     [-238, -217, -183, -166, -176]

I am learning erlang.

This code returns a list of binaries (each of length 2 bytes), but I 
haven't been able to make the last step of converting to ints.

     b2i(Data) ->
         X = b2iacc(Data, []).
         io:format("d2c: ~p~n", [X]),
         X.

     b2iacc(<<H:16, R/binary>>, Out) ->
         X = <<H:2/little-signed-integer-unit:8>>,
         b2iacc(R, [X | Out]);
     b2iacc(<<>>, Out) ->
         lists:reverse(Out).

Am I nearly there, or am I barking up the wrong tree?

With thanks and best wishes

Ivan

-- 
============================================================
Ivan A. Uemlianin
Speech Technology Research and Development

                     ivan@REDACTED
                      www.llaisdy.com
                          llaisdy.wordpress.com
                      www.linkedin.com/in/ivanuemlianin

     "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"
                      (Schiller, Beethoven)
============================================================



More information about the erlang-questions mailing list