Split a string into several integer

Hey there, I am currently working on a small projekt with an arduino and an ESP8266 (thus C code) where I have a small problem.
I have a "simple" problem: I get an char *dataBuf full of data in the format "number,number,number,number" and need to extract this into 4 different integer values. All while testing if there is an error somewhere (string might actually be formatted wrong due to some issues). I guess strtok() could be used to split it up and atoi() to convert. I can't seem to come up with some elegant code here though.

Could anyone help me out with a bit of code? Thanks in advance!

Parsing is never simple, and rarely elegant. Don't judge yourself too harshly.

strtok() and atoi() is probably the best way to do it.

Regards,
Ray L.

The parse example in serial input basics may be helpful.

...R

Thanks for your help! The serial input basics are interesting and I tried to use that.
I wrote some code now to test this part but I am getting strange results. There are a lot of printf's for testing and the "part" prints the right result (123 345 789 321). The xxx part is where I struggle though. using r = atoi(part) always gives me 123 for every int (r=g=b=...=123). Where is the error?

Whats the best way to catch errors here if the string isn't of the right form?

#include <stdio.h>
#include <stdlib.h>
#include <cstring>

int main(void) {

	const char *data = "123,456,789,321";
	int data_len = 20;
	char *dataBuf	= (char*)malloc(data_len+1);
	memcpy(dataBuf, data, data_len);
	dataBuf[data_len] = 0;

	printf("Running ...\r\n");

	char delimiter[] = ",";
	char *part;
	int r,g,b,brightness;

	part = strtok(dataBuf, delimiter);

	int i = 0;
	printf("i is now: %i , ", i);
	while (part != NULL) {
		switch(i) {
			case 0:
				printf("part is now: %s , ", part);
				r = // xxx
				printf("r is: %i, ", r);
				i++;
				part = strtok(NULL, delimiter);
				printf("i is now: %i , ", i);
				break;

			case 1:
				printf("part is now: %s , ", part);
				g = // xxx
				printf("g is: %i, ", r);
				i++;
				part = strtok(NULL, delimiter);
				printf("i is now: %i , ", i);
				break;

			case 2:
				printf("part is now: %s , ", part);
				b = // xxx
				printf("b is: %i, ", r);
				i++;
				part = strtok(NULL, delimiter);
				printf("i is now: %i , ", i);
				break;

			case 3:
				printf("part is now: %s , ", part);
				brightness = // xxx
				printf("brightness is: %i\r\n", r);
				i++;
				part = strtok(NULL, delimiter);
				printf("i is now: %i , ", i);
				break;
		}
	}

	return EXIT_SUCCESS;
}

What's wrong with this picture?

printf("g is: %i, ", r);

and this one:

printf("b is: %i, ", r);

and this one:

printf("brightness is: %i\r\n", r);

Regards,
Ray L.

As I said there is no easy way to check for errors. You just need to look at each piece and make sure it is correct and makes sense.

If it is supposed to be a command, you can check it against a dictionary of the commands.

If it is supposed to be a number, you can do an isdigit() on each character to make sure there are no bad values. When you convert it from text to a number, you can check the value to make sure it is reasonable. Because humans are messy and unpredictable, your error checking needs to be messy and predictable. 8^)

@Ray: I new it 8) Thanks! There is always a stupid error you dont see yourself :wink:

KeithRB:
As I said there is no easy way to check for errors. You just need to look at each piece and make sure it is correct and makes sense.

If it is supposed to be a command, you can check it against a dictionary of the commands.

If it is supposed to be a number, you can do an isdigit() on each character to make sure there are no bad values. When you convert it from text to a number, you can check the value to make sure it is reasonable. Because humans are messy and unpredictable, your error checking needs to be messy and predictable. 8^)

You could also use strtol() in place of atoi(). It can tell you where the first invalid (non-numeric) character in the string is.