3

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.

j.w.r
  • 4,136
  • 2
  • 27
  • 29
Nathan Chan
  • 177
  • 1
  • 3
  • 14
  • 3
    look in http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java – gtgaxiola Sep 17 '12 at 18:14
  • Or this related [example](http://stackoverflow.com/a/2212025/230513). – trashgod Sep 17 '12 at 18:18
  • [link] http://stackoverflow.com/questions/11437203/byte-array-to-int-array http://stackoverflow.com/questions/7619058/convert-a-byte-array-to-integer-in-java-and-vise-versa http://stackoverflow.com/questions/1026761/how-to-convert-a-byte-array-to-its-numeric-value-java – Alvin Pradeep Sep 17 '12 at 18:25

3 Answers3

2

You can use

ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN or BIG_ENDIAN);
bb.position(pos);
int n = bb.getInt();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
byte array - > String -> Integer
  1. Byte array - 4 bytes;
  2. String - new String(byte array);
  3. Integer - Integer.parseInt(String);
Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45
  • This assumes that the String encoding of an integer is the same as how it would be stored in a byte array - which is incorrect. For example, `new String(new byte[] {65})` returns `A`. – matt b Sep 17 '12 at 18:48
0

Well, here's a general way to do it; it it's a 32-bit Big-Endian (the usual 'network order'), starting at position 'pos' in your array:

int out = 0;
out += 0xFF & data[pos++];
out <<= 8;
out += 0xFF & data[pos++];
out <<= 8;
out += 0xFF & data[pos++];
out <<= 8;
out += 0xFF & data[pos++];

But this can be adapted to the number of bytes used for your integers. I'd make methods to call, returning 'out'. Look out for bugs due to sign. The "0xFF &" is there to avoid those. Also, not sure I got the <<= thing right.

If they're little-endian, well a bit harder:

int out = 0;
pos += 4;
out += 0xFF & data[--pos];
out <<= 8;
out += 0xFF & data[--pos];
out <<= 8;
out += 0xFF & data[--pos];
out <<= 8;
out += 0xFF & data[--pos];

These are just one way to do it. (Disclaimer again: untested.)

Jonas N
  • 1,757
  • 2
  • 21
  • 41