# Convert UDP packet to floats?

**URL:** <https://discourse.processing.org/t/convert-udp-packet-to-floats/27843>\
**Category:** Libraries\
**Created:** [February 15, 2021, 6:01pm UTC](https://discourse.processing.org/t/convert-udp-packet-to-floats/27843 "2021-02-15T18:01:44Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![jetjaguar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jetjaguar/32/1370_2.png) [@jetjaguar](https://discourse.processing.org/u/jetjaguar)\
**Post date:** [February 22, 2021, 8:05pm UTC](https://discourse.processing.org/t/convert-udp-packet-to-floats/27843/18 "2021-02-22T20:05:17Z")

</div>

I did see references to little endian, and those are the exact numbers I was expecting for those bytes, thank you! I think I’m starting to understand it now that I see all of the parts working together.

Here is the final code, but I will also start a new thread with this code in case anyone wants to do something similar ([here](https://discourse.processing.org/t/live-face-tracking-sans-opencv/28010)).

```auto
import hypermedia.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

UDP udp;
int PORT_RX= 7000; //port : set in opentrack
String HOST_IP="192.168.86.28"; // must be computer IP address, use ipconfig to find
String receivedFromUDP = "";
int x,y,z;

void setup() {
  size(200, 100);
  udp= new UDP(this, PORT_RX, HOST_IP);
  //udp.log(true);
  udp.listen(true);
  super.start();
}

void draw() {
  background(0);
  textSize(20);
  text(x, 20, 30);
  text(y, 20, 50);
  text(z, 20, 70);
}

void receive(byte[] data) {    
  ByteBuffer bb = ByteBuffer.allocate(data.length);
  bb.order(ByteOrder.LITTLE_ENDIAN);
  bb.put(data);
  bb.rewind();
  x = (int) bb.getDouble();
  y = (int) bb.getDouble();
  z = (int) bb.getDouble();
  //println("x,y,z: " + xx + ", " + yy + ", " + zz);
}

float byteArrayToFloat(byte[] data) {
  int intBits = 
    data[3] << 24 | (data[2] & 0xFF) << 16 | (data[1] & 0xFF) << 8 | (data[0] & 0xFF) |
    data[4] << 32 | (data[5] & 0xFF) << 40 | (data[6] & 0xFF) << 48 | (data[7] & 0xFF) << 56;
  return Float.intBitsToFloat(intBits);
}

```

---

_[View the full topic](https://discourse.processing.org/t/convert-udp-packet-to-floats/27843)._
