ayrdim

ayrdim

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.

Showing Posts 1 to 10

ChrisYammine

ChrisYammine

Does this do what you need? erlang — OTP 29.0.2 (erts 17.0.2)

ayrdim

ayrdim OP

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

ChrisYammine

ChrisYammine

Looking at the docs for <<>>/1 I’m surprised what you have in your example doesn’t work

If you try to inspect flt it just prints out the bitstring?

Edit: sorry I didn’t read properly – your example works you’re just asking if there are alternatives! Not to my knowledge :sweat_smile:

D4no0

D4no0

I am not sure why you would want an alternative to binary pattern match, if you read the documentation it has the possibility to decode pretty much you can think of.

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}
ayrdim

ayrdim OP

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)

ayrdim

ayrdim OP

Looks like pattern matching is the go to way of decoding bytes/bits - Using it like this meets my use case (cant believe I didn’t think of this). Thanks!

D4no0

D4no0

Once you get the hangs on how to use binary pattern match, you will not want to go back to do this with functions, as it is cleaner, less code and much more readable.

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:

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? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews