bwanab

bwanab

convert binary floating and doubles in IEEE-754 format.

Hi. I’m new to elixir, so this could be obvious. I’m reading and writing to a device that uses a binary line format. The format is fixed. It also has integer fields, but they are easy to interpret. Floats and doubles are a bit harder, so I’m wondering and hoping that this is a problem that’s already been solved.

Here’s an example of what I’m looking at:

SSSSSSSSSSSSIIIIIIIIFFFFDDDDDDDD

Where S…S is a string descriptor.
IIII is an integer that I unpack with ((i1 <<< 32) + (i2 <<< 16) + (i3 <<< 8) + i4)

The ones I’m struggling with are FFFF (32 bit float) and DDDDDDDD (double prec float).

I know I can look at the IEEE-754 spec and figure this out, but I’m thinking there might be a solution already. For example in python given the FFFF, I can use struct.unpack(FFFF) to get the floating point value.

Most Liked

hauleth

hauleth

Just remember that binary pattern matching also supports integers, so instead of manual addition and shifts you can do:

<<describe::binary-size(12), i1::little-integer-32, i2::little-integer-32, f1::float-32, f2::float-64>> = input

That is why I love working with binary data in Erlang. It is just so enormously easy.

NobbZ

NobbZ

This should totally do it for you, unless you have to deal with NaN and infinties…

iex(1)> bin = <<1.22::float>>         
<<63, 243, 133, 30, 184, 81, 235, 133>>
iex(2)> <<x :: float>> = bin
<<63, 243, 133, 30, 184, 81, 235, 133>>
iex(3)> x
1.22
iex(4)> bin = <<1.22::float-size(32)>>
<<63, 156, 40, 246>>
hauleth

hauleth

It is in docs for <<>>/1.

ityonemo

ityonemo

probably not an issue, but if things go south don’t forget to check the endianness (there are extra decorators for that if you need it).

Where Next?

Popular in Questions Top

Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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

We're in Beta

About us Mission Statement