I have a problem regarding a string of signs and its values. I would like to get data on each piece of the string and save it into its own buffer. For example from this string:
char str = "+TY123-UP65.4-FA545-MTE565-MTD65-MTT230-MPE545-MPD656-MPT345-";
I want to compare characters after each sign has taken its place. First is the "+" sign that indicates a new string of data. Then I want to compare each first two or three values before the "-" sign comes up (it indicates end of section) and if it's, for example, TY before the number save the number (int or float) following to the "TYbuffer" till the "-" sign. Then after the "-" check again for which letters are first and if it is "UP" save it into "UPbuffer" and so on.
First, i go through the string like this:
size_t len = strlen(str);
size_t i;
for (i=0;i<len;i++){ //go through the string
int j=0;
if (str[j] == '+' && str[j+1]=='T'){ //Check for the letters
}
}
This is an example of how I would do it but the problem is that numbers can be larger or smaller so the positions can be shifted. I am a bit confused on solving the problem. I tried with extracting the numbers from the string but then the person doesn't know from which specific section did it came from. Like this:
char tmp[20];
void loop() {
char *str = "+TY123-UP65.4-FA545-MTE565-MTD65-MTT230-MPE545-MPD656-MPT345-";
char *p = str;
while (*p) {
if (str == 'T'){
while (*p) {
if (isdigit(*p)) { // Upon finding a digit
long val = strtol(p, &p, 10); // Read a number
sprintf(tmp, "%1d", val);
Serial.println(val); // and print it
} else {
p++;
}
}
For the reference, I'm using an Arduino platform.
I've managed to extract the int/float values from the string with a bit of an Error for which I have to take care of. I got rid of the "+" sign at the beginning. Here is the further code:
#include <stdlib.h>
void setup() {
Serial.begin(9600);
char str[] = "TY123-UP65.4-FA545-MTE565-MTD65-MTT230-MPE545-MPD656-MPT345-";
Serial.write(str);
analyzeString(str);
}
void loop() {
}
void analyzeString(char* str) {
int count = 0; //start the count
char buff[20]; //initialiye the buffer for 20 char
for(int i = 0; i<strlen(str); i++) { //start the loop for reading the characters from whole string
if(str[i] == '-') { //start the loop when "-" occurs
char bufNum[20]; //buffer for the number in a seperate section
//char bufNum1[20];
if (buff[0] == 'T' && buff[1] == 'Y') { //If the first letter in a buffer is U and second is P
for(int j = 2; j<count; j++) {
bufNum[j-2] = buff[j];
}
bufNum[count-2] = '\0';
Serial.println();
int ty = atoi(bufNum); //atof(string) for float, atoi(string) for int
Serial.print(ty); //float constant
} else if (buff[0] == 'U' && buff[1] == 'P'){
for(int j = 2; j<count; j++) {
bufNum[j-2] = buff[j];
}
bufNum[count-2] = '\0';
Serial.println(bufNum);
float up = atof(bufNum);
Serial.print(up);
} else if (buff[0] == 'F' && buff[1] == 'A'){
for(int j = 2; j<count; j++) {
bufNum[j-2] = buff[j];
}
bufNum[count-2] = '\0';
Serial.println(bufNum);
int fa = atoi(bufNum);
Serial.print(fa);
}
count =0;
} else {
buff[count++] = str[i];
}
}
}
The error is in the output because it outputs the next values of the sections like this:
TY123-UP65.4-FA545-MTE565-MTD65-MTT230-MPE545-MPD656-MPT345-
12365.4
65.40545
545
I'm looking for a guide on how should I approach the problem. I would appreciate any help. Thank you.