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.

ayrdim

ayrdim

Hey Chris, Thanks for the reply! I’ve given that a try and it still seems to also only parse/decode the string representations of floats. I am more after something that will take in some bytes that are in ieee floating point format and return a float

ayrdim

ayrdim

I guess I also asked the question partly due to a bit of curiosity/confusion as there are standard decode/parsing functions for things such as strings (i.e. Integer.parse(string), Float.parse(string)), however there didnt really seem to be a set of consistent function that did this for bytes - I could only find :binary.decode_unsigned(bytes)

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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

We're in Beta

About us Mission Statement