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.
Trending in Questions
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
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
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ChrisYammine
Does this do what you need? erlang — OTP 29.0.2 (erts 17.0.2)
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
ChrisYammine
Looking at the docs for
<<>>/1I’m surprised what you have in your example doesn’t workIf you try to inspect
fltit 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
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
You can easily wrap it in a function:
Here’s a version that just parses a float from the front and returns the rest of the binary:
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)
ayrdim
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
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
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
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.