We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there
I am reading a string from serial port (from Arduino) and need to convert it to long. My code throws an exception every time - and catches it. Isn't the Long.parseLong(String) the way to parse string to long? :
String val; //global variable
[..]
and in draw():
if ( myPort.available() > 0)
{ // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
println("Val=" + val);
myPort.clear();
}
// While Java's parseLong() throws an Exception:
try {
println("long l=" + Long.parseLong(val));
} catch (NumberFormatException e) {
println(val);
println("can't be converted to a number!");
}
exit();
Many thanks in advance
Answers
Clean up the reading w/ trim():
https://processing.org/reference/trim_.html
Ah thanks.