|
|
View previous topic :: View next topic |
Author |
Message |
macgyver
Joined: 24 Aug 2005 Posts: 12
|
string parsing |
Posted: Wed Aug 24, 2005 5:49 am |
|
|
hi.
from a PC to a PIC i am looking to take in a string of data which will look simlar to X17264 Y74747 Z78689 or the letters at the end of the numbers if it's easier, but what i need is to be able to take the numbers out an put them into the appropiate variable namely X,Y and Z. anyone have an idea as to how to do it, bare in mind i am no programmer, the rest of my code works fine but doing this is over my head.
thanks. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Aug 24, 2005 6:25 am |
|
|
Take a look at this:
http://www.ccsinfo.com/forum/viewtopic.php?t=20225&highlight=sscanf
Another approach would be to look at each character. If you find an X, then start reading each char verifying that its a number. When you find one that is not, convert the values read into a number.
Here some untested code for you
Code: |
int8 getnumber(char* Buf, int8 index, long* retval)
{
char number[10];
int8 i;
// Look for digits
i = 0;
while (isdigit(Buf[index]) && i< (sizeof(number)-1))
{
// Store the digits into a temp array
number[i] = Buf[index];
i++;
index++;
}
// terminate the string
number[i] = 0;
*retval = atol(number);
// return the new position in the array
return(index);
}
void main(void)
{
#define BUF_SIZE 25
char buf[BUF_SIZE]={"X17264 Y74747 Z78689"};
long x,y,z;
int8 i;
for (i=0;i<BUF_SIZE;i++)
{
if (buf[i] == 'X')
{
i++;
i = getnumber(buf,i, &x);
}
else if (buf[i] == 'Y')
{
i++;
i = getnumber(buf,i, &y);
}
else if (buf[i] == 'Z')
{
i++;
i = getnumber(buf,i, &z);
}
}
while(1);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|