Detect if part of an input are numbers?

Hey folks

I have a program that translates a program into a code, and then needs to respond to certain commands. One of these is "LPXXXXXXXXXXXX" Where the X's must be digits (which will then relate to LED Intensity)

How would I go about checking if all the X's are digits before continuing? The only way I can think is a 12-layer nested if statement which is far from ideal! :stuck_out_tongue:

Thanks,

Harry

'0' == 48
'9' == 57

There's also the isdigit function.

boolean isnumber = false;
for (unit8_t i=0; i<12 && isnumber; i++)
{
  isnumber = (data[i+2] >= '0' && data[i+2] <= '9');
}
if (isnumber)
 // they are all digits ...

marco_c:

for (unit8_t i=0; i<12 && isnumber; i++)

I think you meant uint8_t.

Typo.

marco_c:
Typo.

Compilers have zero tolerance for typos.

@ThatAdamsGuy, the examples in Serial Input Basics receive all the data before trying to analyse any of it. There is also a parse example to illustrate converting ascii into integers and floats.

If you have control over the data format being sent to the Arduino I suggest you use the system in the 3rd example.

If your LP is followed by a single number (represented by all the Xs in your example) you could just try using the atoi() function on that part.

If it is important to ensure that every one of the Xs is an ascii character between 0 and 9 then you could just iterate over that part of the array and check for values above 57 or below 48

You have not said what should happen if a character is not a digit, or how it might be possible for one of them not to be a digit. Those issues can also be relevant to the design of the program.

...R