# Float value to Byte array

**URL:** https://discourse.processing.org/t/float-value-to-byte-array/16698
**Category:** Coding Questions
**Created:** [December 29, 2019, 10:44am UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698 "2019-12-29T10:44:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [December 29, 2019, 10:44am UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/1 "2019-12-29T10:44:49Z")

</div>

Hello there how to convert float val to byte array ? i have this function that work in oposite way, it converts Byte array to Float value:

```auto
public float byteArrayToFloat(byte test[]) {
  int MASK = 0xff;
  int bits = 0;
  int i = 3;
  for (int shifter = 3; shifter >= 0; shifter--) {
    bits |= ((int) test[i] & MASK) << (shifter * 8);
    i--;
  }
  println(bits);
  return Float.intBitsToFloat(bits);
}

```

So… How to make **floatToByteArray** function?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 29, 2019, 12:25pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/2 "2019-12-29T12:25:34Z")

</div>

> [@GeorgeJava](#):
>
> floatToByteArray

A search for " **floatToByteArray**" is a good start.

I did this a while back and was able to come up with solutions.

🙂

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 29, 2019, 12:29pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/3 "2019-12-29T12:29:02Z")

</div>

This is my take on it for both opposite conversion ways. ↔  
You can also take a look at this site below in order to double-check results: ✔

- [H-Schmidt.net/FloatConverter/IEEE754.html](http://H-Schmidt.net/FloatConverter/IEEE754.html)
- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#floatToIntBits(float)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#floatToIntBits(float))

```auto
/**
 * Float As Bytes Array (v1.1.1)
 * GoToLoop (2019/Dec/29)
 * https://Discourse.Processing.org/t/float-value-to-byte-array/16698/3
 */

static final float MY_FLOAT = -PI;

byte[] floatBytes;
float convertedFloat;

void setup() {
  println(MY_FLOAT);

  floatBytes = floatToByteArr(MY_FLOAT);
  for (final byte b : floatBytes) print(hex(b), TAB);
  println();

  convertedFloat = byteArrToFloat(floatBytes);
  println(convertedFloat, TAB, convertedFloat == MY_FLOAT);

  exit();
}

static final byte[] floatToByteArr(final float f) {
  return byte(floatToIntArr(f));
}

static final int[] floatToIntArr(final float f) {
  final int i = Float.floatToIntBits(f);
  return new int[] { i >>> 030, i >> 020 & 0xff, i >> 010 & 0xff, i & 0xff };
}

@SafeVarargs static final float byteArrToFloat(final byte... b) {
  return b != null? intArrToFloat(int(b)) : MIN_FLOAT;
}

@SafeVarargs static final float intArrToFloat(final int... i) {
  if (i == null || i.length < Float.BYTES) return MIN_FLOAT;
  return Float.intBitsToFloat(i[0] << 030 | i[1] << 020 | i[2] << 010 | i[3]);
}

```

---

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [December 29, 2019, 7:00pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/4 "2019-12-29T19:00:56Z")

</div>

> [@GoToLoop](#):
>
> @SafeVarargs

Why is there @SafeVarargs ?

> [@GoToLoop](#):
>
> i \>\>\> 030

And difference between “\>\>” and “\>\>\>” ?

> [@GoToLoop](#):
>
> i \>\>\> 030, i \>\> 020 & 0xff, i \>\> 010

… And why is the shifts written as “030”, “020” and “010” ? Maybe iam not the only one who wants to know 😛

---

<div class="post-metadata">

### Author: ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)
#### Post date: [December 29, 2019, 7:14pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/5 "2019-12-29T19:14:34Z")

</div>

ahh whe you place **zero “0”** before number, it is in the octal number system, thats it

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [December 29, 2019, 7:44pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/6 "2019-12-29T19:44:24Z")

</div>

> [@GeorgeJava](#):
>
> And difference between “\>\>” and “\>\>\>” ?

> **[Bitwise Right Shift Operators in Java - GeeksforGeeks](https://www.geeksforgeeks.org/bitwise-shift-operators-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

🙂

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 29, 2019, 8:24pm UTC](https://discourse.processing.org/t/float-value-to-byte-array/16698/7 "2019-12-29T20:24:47Z")

</div>

> [@GeorgeJava](#):
>
> Why is there @SafeVarargs?

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/SafeVarargs.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/SafeVarargs.html)

> [@GeorgeJava](#):
>
> And difference between `>>` and `>>>`?

Bit-shift operator `>>>` shifts the most significant bit (negative bit), so the value isn’t negative anymore.

> [@GeorgeJava](#):
>
> And why is the shifts written as “030”, “020” and “010”?

`println(030, 020, 010); // 24, 16, 8 (Octal)`
