Hi, I've just written a simple function to help me strip error numbers out of a string, after finishing it, it's occurred to me that there's probably a much simpler one or two line solution.
I'm starting with a string (actually an array of bytes) contain text something like this "+SIND: 7 +CME ERROR: 23 +SIND: 3"
Given the rough position of the number I'm interested in, plus the characters before and after it, I can strip the actual number out of the string with the following function....
/***************************************************************************
Parses part of string for a number. This is used for striping data and
error numbers out of results passed back from the satellite modem.
Consider a string as follows... "+CSQ: 0,99 /n/rOK/n/r+SIND: 3/n/n/rOK/n/r"
This function could strip out the 0,99 or 3.
startPos = position to start procesing string from.
startChar = character preceeding the number being looked for.
endChar = character following the number being looked for.
***************************************************************************/
int SatModem::parseStringForNum(int startPos, byte startChar, byte endChar)
{
int startCharPos=0;
int endCharPos=0;
char temp[4+1]; // Temp buffer for the numeric part of a string only
int n=0;
// Get the start and end indexes of the section of the string with the number in it.
for (int x = startPos ; x<=COMM_BUF_LEN ; x++ )
{
if (comm_buf[x] == startChar)
{
startCharPos=x+1;
}
if ((startCharPos!=0) && (comm_buf[x] == endChar) )
{
endCharPos=x-1;
}
if (comm_buf[x]==0)
{
break;
}
}
// Copy the target bytes into a temporary string
for (int x = startCharPos ; x<=endCharPos ; x++ )
{
temp[n]=(char)comm_buf[x];
temp[n+1]=0;
n++;
}
// Convert to an integer
return atoi(temp);
}
This does work just fine, but I can't help feeling there's probably a much more elegant solution. (something that doesn't involve the temporary array) Any suggestions?
Love a little Saturday night code golf. Here's an old-school approach:
int parseNum(char *buf, int pos) {
char *parseptr = buf + pos;
while (*parseptr && !isdigit(*parseptr)) ++parseptr;
int result = 0;
while (isdigit(*parseptr)) result = (result * 10) + *parseptr++ - '0';
return result;
}
Not really anything of technical value. So what are your specific issues in this instance?
The simple fact that it is being used. The technical issues have been discussed many many times in the past.
In brief: String objects are fine on a computer where a typical string takes a tiny tiny percentage of the total memory, but on a microcontroller, where a string may be 10% of the total memory the String object is the main cause of memory corruption / fragmentation and randomly crashing Arduinos.
Copying string data into a String object just for the purposes of using some of the member functions that can just as easily be done using the standard C functions that manipulate the string directly is a massive waste of memory.
(note: "String" is a String object instance. "string" is a C string composed of a null-terminated array of characters.)
Highly technical response. Sounds of a religious/superstitious nature. The issue most of the anti String types have touted has been corrected in IDE 1.0.4, although I never saw an actual case where simple code was ever impacted by using Strings. Perhaps you can find something else that goes bump in the night for entertainment.
Highly technical response. Sounds of a religious/superstitious nature. The issue most of the anti String types have touted has been corrected in IDE 1.0.4, although I never saw an actual case where simple code was ever impacted by using Strings. Perhaps you can find something else that goes bump in the night for entertainment.
Well, I'm sorry you feel that way, I truly am.
I have had arguments with people like you about Strings vs strings in the past, and at the first hint of them being wrong they just turn to personal insults.
One single issue with the dynamic memory functions has been "cured" in 1.0.4. That doesn't "cure" the String object, and it doesn't "cure" the simple fact that dynamic memory usage on any small microcontroller with very limited RAM is inherently a bad idea.
I suggest you come back to me when you are capable of having a rational argument. I suggest you first learn a few basic things about memory management - what is the difference between the stack and the heap. How big is the heap? What happens when the stack and the heap collide? What actually happens when you use something like String.subString() etc. You know, basic knowledge of how these things work, so that we can then actually discuss it as opposed to the voices in your head telling you things. Oh, I forgot - when you do have said knowledge you won't need to come back and discuss it, as you will then know yourself how wrong Strings are.