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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement