i'm currently working on a project that sends data from a java application through a serial port to an arduino.
The problem i have is the following, i need to split an Integer into 2 bytes and then combine them into an Integer in Arduino. But the other way around (Arduino->java) only causes trouble for me. The arduino part isn't that hard and works like a charm, but despite me looking through the relevant Questions&Answers already posted on here, i can't quite work out how to combine the bytes correctly together into an int.
Here's the java code that just refuses to work :
int in = 500; byte[] data = new byte[2]; data[0] = (byte)(in & 0xFF); data[1] = (byte)((in >> 8) & 0xFF); int res = data[0] | (data[1] << 8); The console print out i get from this is:
data[0] = -12 data[1] = 1 res = -12 but i need res to be 500!
data[0], 0 indata[0]. What about the other 0. Shouldn't it bebyte[3]?byte[]. If you use ashort[]or anint[]it would work as expected