# How to convert hex to float?

**URL:** <https://community.openhab.org/t/how-to-convert-hex-to-float/141009>\
**Category:** Scripts & Rules\
**Tags:** rules, hex, float\
**Created:** [November 14, 2022, 10:33am UTC](https://community.openhab.org/t/how-to-convert-hex-to-float/141009 "2022-11-14T10:33:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martiniman](https://community.openhab.org/user_avatar/community.openhab.org/martiniman/32/60179_2.png) [@martiniman](https://community.openhab.org/u/martiniman)\
**Post date:** [November 14, 2022, 10:33am UTC](https://community.openhab.org/t/how-to-convert-hex-to-float/141009/1 "2022-11-14T10:33:31Z")

</div>

Hi! How to convert hex to float?

```php
        var String output = "67|FA|40|48"
        var String[] bytes = output.split("\\|") // 67FA4048
        var Number r = Integer.parseInt(bytes.get(2) + bytes.get(3) + bytes.get(0) + bytes.get(1), 16).floatValue as Number
		logInfo("test", r.toString)

```

Result is 1.07848704E9

But right result on picture:  
 ![hex](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/f/7/f7074094b5404f01507bd07314c61efaa7116221.jpeg)

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [November 14, 2022, 11:37am UTC](https://community.openhab.org/t/how-to-convert-hex-to-float/141009/2 "2022-11-14T11:37:36Z")

</div>

First you must find out how your float is encoded. 7 bytes is unusual.

---

<div class="post-metadata">

**Author:** ![martiniman](https://community.openhab.org/user_avatar/community.openhab.org/martiniman/32/60179_2.png) [@martiniman](https://community.openhab.org/u/martiniman)\
**Post date:** [November 14, 2022, 11:39am UTC](https://community.openhab.org/t/how-to-convert-hex-to-float/141009/3 "2022-11-14T11:39:59Z")

</div>

Encoded as CDAB, i used [online converter](https://www.scadacore.com/tools/programming-calculators/online-hex-converter/) to find out right result 3.13134623

---

<div class="post-metadata">

**Author:** ![martiniman](https://community.openhab.org/user_avatar/community.openhab.org/martiniman/32/60179_2.png) [@martiniman](https://community.openhab.org/u/martiniman)\
**Post date:** [November 14, 2022, 4:58pm UTC](https://community.openhab.org/t/how-to-convert-hex-to-float/141009/4 "2022-11-14T16:58:19Z")

</div>

Here is solution

```php
var String parse = bytes.get(2) + bytes.get(3) + bytes.get(0) + bytes.get(1)
var int n = Integer.parseInt(parse, 16) 
var Float convert = Float.intBitsToFloat(n)

```
