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
Hello,
You could try something like that :
data = IO.read(file, 2)
array = for <<part::bytes-size(2) <- data>>, do: :binary.decode_unsigned(part)
0
Most Liked
josevalim
Creator of Elixir
You should also be able to get an integer directly too:
for <<part::integer-unsigned-size(2) <- data>>, do: part
4
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
1
Popular in Questions
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










