I'm starting out and trying to get some communication going on between Processing and a Python script. I've got things set up on the Python side and it's sending some data over a UDP socket connection.
I've got the UDP library working alright in Processing as well, and I'm able to receive data from the socket.
Problem is, the way I've done it (cobbled together from various examples and tutorials), it seems to be encoded as an array of number pairs (is that UTF-8?). What I'm stumped with is how to convert the input from the socket back into the original string.
I've used a couple examples that I found, unfortunately the credits / original authors escape me at the moment.
import hypermedia.net.*;
int PORT_RX=9000; //port
String HOST_IP="127.0.0.1"; //ip address
UDP udp;
String receivedFromUDP = "";
void setup() {
size(400,400);
udp= new UDP(this,PORT_RX,HOST_IP);
udp.log(true);
udp.listen(true);
super.start();
}
void draw() {
background(0);
text(decode(receivedFromUDP), 50, 50);
}
void receive(byte[] data, String HOST_IP, int PORT_RX) {
receivedFromUDP = "";
for (int i = 0; i < data.length; i++) {
receivedFromUDP += str(data[i])/* + " "*/;
}
println(data);
}
String decode(String s)
{
String eu = s.substring(10);
byte[] ba = javax.xml.bind.DatatypeConverter.parseBase64Binary(eu);
try
{
return new String(ba, "UTF-8");
}
catch (Exception e)
{
println(e);
}
return null; // Only if we had the exception, which shouldn't happen if you don't change the encoding...
}
The Python bit is sending a pair of floats separated by a comma, what I'm getting in Processing is an array of numbers that - I'm assuming - corresponds to the Python data...