First, let’s say, that n_bytes = 2 and we do not need an array, then it’s pretty simple to write in elixir:
<<data :: bytes-size(2), _ :: binary>> = file
res = :binary.decode_unsigned(data)
Let’s return to python line:
It returns 2-byte array of numbers. ‘H’ means 2 byte.
So the question is, how to make the same in elixir?
Thanks in advance
You could try something like that :
data = IO.read(file, 2)
array = for <<part::bytes-size(2) <- data>>, do: :binary.decode_unsigned(part)
You should also be able to get an integer directly too:
for <<part::integer-unsigned-size(2) <- data>>, do: part
Thanks for your response. It might be:
for <<part::integer-unsigned-size(16) <- data>>, do: part
As size means bits, not bytes. By the way integer-unsigned-size is big endian. Any way to code the same with little endian?
Thank, that works, as you can specify big and little endian.
Yes you can add the endianness directly in the form @josevalim gave :
for <<part::little-integer-unsigned-size(16) <- data>>, do: part
All possibilities are described here: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#<<>>/1