Houdini

Houdini

Analogue of python's: array.array('H', file.read(n_bytes))

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: array.array('H', file.read(n_bytes))
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

First Post!

lud

lud

Hello,

You could try something like that :

data = IO.read(file, 2)
array = for <<part::bytes-size(2) <- data>>, do: :binary.decode_unsigned(part)

Most Liked

josevalim

josevalim

Creator of Elixir

You should also be able to get an integer directly too:

for <<part::integer-unsigned-size(2) <- data>>, do: part
lud

lud

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: Kernel.SpecialForms — Elixir v1.20.2

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement