# How to convert binary data with primitive data types

**URL:** <https://forum.elixirforum.com/t/how-to-convert-binary-data-with-primitive-data-types/62031>\
**Category:** Questions\
**Tags:** binary\
**Created:** [March 1, 2024, 10:53pm UTC](https://forum.elixirforum.com/t/how-to-convert-binary-data-with-primitive-data-types/62031 "2024-03-01T22:53:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [March 2, 2024, 12:45am UTC](https://forum.elixirforum.com/t/how-to-convert-binary-data-with-primitive-data-types/62031/2 "2024-03-02T00:45:31Z")

</div>

Have you checked for endianness? Erlang assumes big-endian-arranged bytes when extracting floats the way you are doing it.

Demo:

```elixir
bin = <<:math.pi()::float-32>> <> <<0, 0, 0>>
<<64, 73, 15, 219, 0, 0, 0>>

<<x::float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
3.1415927410125732

<<x::big-float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
3.1415927410125732

<<x::little-float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
-4.03314608963584e16

```

As you can see, by default big endian is used. So if your values are little-endian-arranged, you have to specify that explicitly.

In your example, try changing `float-size(32)` to `little-float-size(32)` and see if it works.

---

_[View the full topic](https://forum.elixirforum.com/t/how-to-convert-binary-data-with-primitive-data-types/62031)._
