ayrdim

ayrdim

What is the Elixir way of decoding/parsing binary data?

What is the Elixir way of decoding/parsing binary data?

I’m currently using the pattern matching (E.g. for a float <flt::float> = <<63, 243, 190, 118, 200, 180, 57, 88>>) to decode/parse bytes, is this the only way to decode/parse binary data? Ideally I would prefer to call a function that takes in bytes and returns the decoded value.
My intuition tells me that Float.parse() and Integer.parse() should perform the same conversion as the example above but they seem to only parse strings.

Marked As Solved

adamu

adamu

You can easily wrap it in a function:

def parse_binary_float(<<flt::float>>), do: flt

Here’s a version that just parses a float from the front and returns the rest of the binary:

def parse_binary_float(<<flt::float, rest::binary>>), do: {flt, rest}

Also Liked

axelson

axelson

Scenic Core Team

This is one of my favorite articles about binary pattern matching in Elixir, it’s really neat how they apply binary pattern matching to the task of parsing a PNG:

Ridtt

Ridtt

Hello friends! I’m about to drop to community, under MIT licence, my library for bidirectional parse/encode communication.

Idea is you declaring shape of your binary data and set of constraints, highly dynamic ones, like dynamic length, item size, items count, dynamic variant dispatching, optionality of fields using callbacks. Callbacks may receive on request fields from current parse routine along with context options from total parse tree.

Most of the time you will work only with type conversion called ‘managed’. Most human value is possible, like number, utf8 string and so on. Does not matter actual data is utf16 and actual numbers are hidden under mask, I have abstractions to keep it complexity managed away from developer.

There is such features as receiving requested type conversion (binary version, for example) and option scoped from particular context interface.

In additional there is feature of virtual field. Virtual fields allow you declare your data as most readable and sane way independent of actual shape of binary.

Everything compiled down to elixir binary pattern matching, using graph topology I’m able to optimise most of blocks with known shape to single pattern and provide requested intermediate values only once, close as possible to usage and passing around only as function arguments, not creating any allocations or intermediate objects.

The most benefits you will get if you working with complex legacy protocol and you need flow like receive → modify → send.

It’s running in our production for few apps for half of year, and currently I’m finishing basic documentation.

Let me know if you guys are interested, I will try to finish basic docs and publish it this week!

trarbr

trarbr

One of the great things about binary pattern matching is that it allows you to decode multiple values (which can be of different type) from a binary in one go. I just gave a talk at FOSDEM that is an introduction to bit syntax (which powers the binary pattern matching). The talk goes through all the basics and has a couple of examples after the 15-minute mark that show how bit syntax can be used to decode multiple different values at once. The talk is available here - maybe it is useful.

And if you need to wrap it in a function, e.g. for use with pipes, @adamu’s approach is definitely valid.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement