# Convert float to bytes?

**URL:** <https://forum.cirrus-link.com/t/convert-float-to-bytes/744>\
**Category:** MQTT Modules\
**Created:** [May 2, 2025, 7:40pm UTC](https://forum.cirrus-link.com/t/convert-float-to-bytes/744 "2025-05-02T19:40:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IgnitionMan](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.cirrus-link.com/ignitionman/32/342_2.png) [@IgnitionMan](https://forum.cirrus-link.com/u/IgnitionMan)\
**Post date:** [May 2, 2025, 7:40pm UTC](https://forum.cirrus-link.com/t/convert-float-to-bytes/744/1 "2025-05-02T19:40:05Z")

</div>

I’m trying to send a float value to the broker, the first error I got was

```auto
system.cirruslink.transmission.publish("BROKER", "ignition/Temperature", event.getCurrentValue().getValue(), 1, 0)

```

```auto
publish(): 3rd arg can't be coerced to byte[]

```

After I added encode(), now I am getting following error:

```auto
system.cirruslink.transmission.publish("BROKER", "ignition/Temperature", event.getCurrentValue().getValue().encode(), 1, 0)

```

```auto
AttributeError: 'float' object has no attribute 'encode'

```

Before I start using different python libraries, is that a standard way to convert a float or integer to bytes similar to .encode()?

---

<div class="post-metadata">

**Author:** ![wes.johnson](https://avatars.discourse-cdn.com/v4/letter/w/f14d63/32.png) [@wes.johnson](https://forum.cirrus-link.com/u/wes.johnson)\
**Post date:** [May 2, 2025, 8:09pm UTC](https://forum.cirrus-link.com/t/convert-float-to-bytes/744/2 "2025-05-02T20:09:10Z")

</div>

Something like this should do:

```auto
import struct

ba = bytearray(struct.pack("f", event.getCurrentValue().getValue()))
system.cirruslink.transmission.publish("BROKER", "ignition/Temperature", ba, 1, 0)

```

---

<div class="post-metadata">

**Author:** ![IgnitionMan](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.cirrus-link.com/ignitionman/32/342_2.png) [@IgnitionMan](https://forum.cirrus-link.com/u/IgnitionMan)\
**Post date:** [May 2, 2025, 8:13pm UTC](https://forum.cirrus-link.com/t/convert-float-to-bytes/744/3 "2025-05-02T20:13:33Z")

</div>

Thanks! I will add that to my library, i also found

```auto
str(event.getCurrentValue().getValue()).encode()

```

Worked!
