[erlang-questions] How to convert a binary to a list of 2 byte ints
Gleb Peregud
gleber.p@REDACTED
Thu May 27 12:58:02 CEST 2010
You can use binary comprehension:
4> X = <<18,255,39,255,73,255,90,255,80,255>>.
<<18,255,39,255,73,255,90,255,80,255>>
5> [ H || <<H:2/little-signed-integer-unit:8>> <= X ].
[-238,-217,-183,-166,-176]
On Thu, May 27, 2010 at 11:31, Ivan Uemlianin <ivan@REDACTED> wrote:
> 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)
> ============================================================
>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
More information about the erlang-questions
mailing list