# How to cast binary array to float array?

**URL:** <https://discuss.python.org/t/how-to-cast-binary-array-to-float-array/46461>\
**Category:** Python Help\
**Tags:** help\
**Created:** [February 20, 2024, 6:27pm UTC](https://discuss.python.org/t/how-to-cast-binary-array-to-float-array/46461 "2024-02-20T18:27:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakib1503052](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rakib1503052/32/18030_2.png) [@Rakib1503052](https://discuss.python.org/u/Rakib1503052)\
**Post date:** [February 20, 2024, 6:27pm UTC](https://discuss.python.org/t/how-to-cast-binary-array-to-float-array/46461/1 "2024-02-20T18:27:45Z")

</div>

Hi! I am trying to read some data coming over serial com port. The data comes as a raw binary float array. Python’ serial module reads the data as string. Now, I need to convert this string to float array. I had been reading up on bytes and struct but still having trouble achieving my objective. To give an example of what I want to do, in C/C++ I would do something like

```python
char Rx_buffer[400];
com_read(Rx_buffer);
float* Rx_data = &Rx_buffer[0];

```

I am looking to do this in python. How can I do it?

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [February 20, 2024, 6:37pm UTC](https://discuss.python.org/t/how-to-cast-binary-array-to-float-array/46461/2 "2024-02-20T18:37:56Z")

</div>

Two functions that may be of help:

> **[struct — Interpret bytes as packed binary data](https://docs.python.org/3/library/struct.html#struct.iter_unpack)**
>
> Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...

[https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html](https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html)

---

<div class="post-metadata">

**Author:** ![Rakib1503052](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rakib1503052/32/18030_2.png) [@Rakib1503052](https://discuss.python.org/u/Rakib1503052)\
**Post date:** [February 21, 2024, 11:48am UTC](https://discuss.python.org/t/how-to-cast-binary-array-to-float-array/46461/3 "2024-02-21T11:48:11Z")

</div>

Thank you very much. I managed to do it.
