I'm trying to make a reliable UDP system and I need to convert byte[] to int and back (JCreator 5.0 LE)
DatagramPacket requires its data in byte[] form, so I have to convert the int information into byte[] so I can send it:
byte[] data = new byte[48];
System.arraycopy(task.getProtocol().byteValue(), 0, data, 0, task.getProtocol().byteValue().length);
System.arraycopy(task.getMessage().byteValue(), 0, data, 4, task.getMessage().byteValue().length);
System.arraycopy(task.getSequence().byteValue(), 0, data, 8, task.getSequence().byteValue().length);
System.arraycopy(task.getAcknowledge().byteValue(), 0, data, 12, task.getAcknowledge().byteValue().length);
for (int i = task.getAcknowledge(); i >= 0 && i > task.getAcknowledge() - 33; i--) {
for (Packet j: tasks) {
if (j.getSequence() == i) {
data[i] = 1;
break;
}
}
}
out = new DatagramPacket(data, data.length, ds.getInetAddress(), portNum);
ds.send(out);
Protocol is the protocolID
Message is the "information" that is being sent
Sequence is the packet's sequence number; the first packet sent has a sequence of 0, the next is 1, and so on
Acknowledge is the acknowledgement of a packet being sent back
The next part is the 32 other acknowledgements. For the sake of saving memory, they are compressed into 1 byte each instead of 4 bytes (int)
Now, when I receive the packet I need to unpackage it. First I need to check the first 4 bytes (the protocol) to see if I will ignore the packet or not, but I don't know how to convert the byte array into an int.