Hello,
I was trying to do something similar, this has been driving me nuts and I finally figured it out. Since this seems to be the only thread on the subject, I thought I'd share my solution here.
I have a Python program that does some very math-ish manipulations of maps. It uses the Python struct module to pack some visualization information into UDP packets and send it to a Processing sketch once in a while so I can watch its progress in a more intuitive form.
At first I was trying to use DataInputStreams, which have methods for reading different data types, using code like this:
Code:byte b;
InputStream in = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(in);
b = din.readByte();
Check the Java documentation if you want to go this route.
But it turns out that Java always uses network byte order (big-endian) for byte streams, and the byte order in my messages was little-endian (they were just packed C structures from an Intel machine) so I started looking at ByteBuffers, which allow you to reverse the byte order before reading from them. However, this was not really the 'right' way to do things since network packets should by convention be in big-endian order. So I fixed my network packets, but still decided to stick with ByteBuffers for the simple interface, and the fact that a buffer seems to be a better fit than a stream for what we're doing.
(Side note for people doing this with Python: if your packets are being generated by the Python struct module, you can add a character '!' to the beginning of the format string to force the contents into network byte order.)
OK I know this is not 'newbie friendly' as the original poster put it, so simplified version is as follows: the order of the bytes in your UDP message is relevant. However, if the message is coming from a standards-compliant application, it will agree with Java/Processing on that order.
On to the example:
Code:import java.nio.ByteBuffer;
void receive( byte[] data, String ip, int port ) throws IOException {
// Java/Processing defaults to read in network byte order.
// Python will send in machine unless you specify in struct.pack().
// Both ByteBuffers and DataInputStreams are good for unpacking data.
int row, col, t;
float x, y, z;
byte b;
ByteBuffer bb = ByteBuffer.wrap(data);
b = bb.get();
if (b == (byte)'T') {
while (bb.hasRemaining()) {
row = bb.get() & 0xFF;
col = bb.get() & 0xFF;
t = (int) (bb.get() & 0xFF);
if (row < 100 && col < 100) ttimes[row][col] += t;
}
} else if (b == (byte)'P') {
while (bb.hasRemaining()) {
row = bb.get() & 0xFF;
col = bb.get() & 0xFF;
x = bb.getFloat();
y = bb.getFloat();
z = bb.getFloat();
if (row < 100 && col < 100) {
targetMesh[row][col].x = x;
targetMesh[row][col].z = y;
targetMesh[row][col].y = z;
}
}
}
}
The &0xFF in my code is to get unsigned values out of the bytes, since Java only does signed values. Again, I know this is not terribly friendly to a beginner, but when you start manipulating byte-level representations of numbers things get slightly messy. But there's lots of info online to figure it all out.
Hope this helps!