Originally Posted by
laserlight
That makes it easy. I'd suggest something like this:
Code:
numBytes = Udp.read(packetBuffer, 255);
if (numBytes != 3) {
// handle unexpected input error
// ...
}
unsigned int result = 100 * (packetBuffer[0] - '0') + 10 * (packetBuffer[1] - '0') + (packetBuffer - '0');
if (result > UCHAR_MAX) {
// handle input value out of range error
// ...
}
tableID = (unsigned char)result;
where the type of numBytes is the return type of Udp.read. You can also do additional error checking to confirm that the content of packetBuffer are digits before you do the computation. Alternatively, you could call strtol instead of doing your own computation, and then check the range.
Thanks for the suggestion! first I used 'char' as type for numBytes, since, the packetBuffer is also char. This gave an error.
So then I tried 'int' since, that is wat I originally use as you can see here in code
Code:
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
checkPackets();
}
This also resulted in error: invalid conversion from 'char*' to 'unsigned int' [-fpermissive]
Where this is the code, used from your suggestion:
Code:
int numBytes = Udp.read(packetBuffer, 255);
if (numBytes != 3) {
// handle unexpected input error
// ...
}
unsigned int result = 100 * (packetBuffer[0] - '0') + 10 * (packetBuffer[1] - '0') + (packetBuffer - '0');
if (result > UCHAR_MAX) {
// handle input value out of range error
// ...
}
tableID = (unsigned char)result;
}
the compile error is on this line:
Code:
unsigned int result = 100 * (packetBuffer[0] - '0') + 10 * (packetBuffer[1] - '0') + (packetBuffer - '0');