Go Down

Topic: help with String manipulation (Read 1 time) previous topic - next topic

GoForSmoke


Yes i understand what you are saying but unfortunately i dont have delimiting characters
the format is 255O255|the part of the code which is handling the parsing does not see the end character "|" which has already been stripped.
i need to be able to use the character position instead.
the examples dont offer any suggestion on how to do this.


Using a 4 char array string you can put the first 3 serial chars in array[0], [1], [2], and make [3] = 0.
Then you have a C string suitable for atoi().

Quote

i need to be able to use the character position instead.
the examples dont offer any suggestion on how to do this.


Make a variable to hold the final number.
**1** Multiple final number variable by 10.
**2** Read the 1st serial char, check that it is >= '0' and <= '9' and if so,
  subtract '0' and add to the final number variable.
Repeat **1** and **2** two more times.
If no errors then you have read 3 digits and your converted value is in the final number variable.

Otherwise rely on black box function until you learn how simple the basis for the above is.

C++ container classes on an UNO is like Formula 1 cars on a half acre dirt track.
Possible but ....

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

johncc

#21
Jan 27, 2013, 08:34 pm Last Edit: Jan 27, 2013, 08:40 pm by johncc Reason: 1

Using a 4 char array string you can put the first 3 serial chars in array[0], [1], [2], and make [3] = 0.
Then you have a C string suitable for atoi().


array and array+4 are already suitable for atoi:

int xxx = atoi(array);
int yyy = atoi(array+4);

John

GoForSmoke

Not without terminating zeros. Go ahead and try.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

johncc


Not without terminating zeros. Go ahead and try.


Sure

Code: [Select]

// Return true if a valid cmd "xxxOyyy|" , false if not
// output parameters xx and yy
bool parse_O_command( const char * str, int & xx, int & yy)
{
// "Input string should arrive in the format of 255O255|"
if ( strnlen(str,8)== 8 && str[3]=='O' && str[7]=='|'){
xx=atoi(str);    // the first 3 characters are the Slave device address
yy=atoi(str+4);    // str[4] and after are digital value
return true;
}
return false;
}


Test Suite
Code: [Select]


void setup(){

int adr, val;
bool valid;

valid = parseOcommand("123O456|", adr, val);
Serial << "1: " << adr << "," << val << " : " << (valid?"valid":"not a valid") << " O command" << endl;

char buf8[8] = {'2','4','6','O','1','8','8','|'};
valid = parseOcommand(buf8, adr, val);
Serial << "2: " << adr << "," << val << " : " << (valid?"valid":"not a valid") << " O command" << endl;

char buf7[7] = {'2','4','6','O','1','7','7'};
valid = parseOcommand(buf7, adr, val);
Serial << "3: " << adr << "," << val << " : " << (valid?"valid":"not a valid") << " O command" << endl;

Serial << "Done" << endl;
}


Output

Code: [Select]
1: 123,456 : valid O command
2: 246,188 : valid O command
3: 246,188 : NOT valid O command
Done



PaulS

Quote
Not without terminating zeros. Go ahead and try.

The initial string is, presumably, NULL terminated. The atoi() function stops processing when it encounters a NULL or when it encounters some non-integer character. So, NULL terminating is not technically required, since it is the original array that is being passed to atoi(), twice.

If the parts of the original array were copied to other arrays, then, the copies would need to be NULL terminated.

Go Up