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.
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
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.
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)
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!
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.
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:
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.
You 100% have option to define that 10. There is more problem with *, but it can be handled. You can also quite easily implement such unpack function on your own