CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Parsing a variable length string.....

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
John Morley



Joined: 09 Aug 2004
Posts: 97

View user's profile Send private message

Parsing a variable length string.....
PostPosted: Thu Mar 10, 2011 1:36 pm     Reply with quote

Hi All,

I need to parse a string that contains fields of variable length. I've got it working, but my "solution" seems to be a bit of a kludge, so I'm hoping to get some other ideas!

I need to parse a string in these formats:

A=xxx, B=yyy<Cr> or A=xxx B=yyy<Cr>

These strings are identical except one has a comma separator, and the other has a space. Note the 'xxx' and 'yyy' portions can be 'x', 'xx', or 'xxx' and 'y', 'yy', or 'yyy'.

To illustrate my technique, I'll just show how I get the first part:

Code:

           for(iIndex = 2 ; Msg_Buffer[iIndex] != ',' && Msg_Buffer[iIndex] != ' ' ; iIndex++)
         {
            DigitCount++;
         }

         // Let's decode the address and act on it.....
         switch (DigitCount)
         {
            case 1:   // here we have a single digit
               RcvAddress = gethexbyte(Msg_Buffer[2]);
            break;
            
            case 2:   // here we have 2 digits
               RcvAddress = (10 *gethexbyte(Msg_Buffer[2])) + gethexbyte(Msg_Buffer[3]);
            break;

            case 3:   // here we have 3 digits
               RcvAddress = (100 * gethexbyte(Msg_Buffer[2])) + (10 * gethexbyte(Msg_Buffer[3])) + gethexbyte(Msg_Buffer[4]) ;
            break;
         }



Again, it's working, but it seems like there must be a 'cleaner' approach? I'd like to do it all in the 'For Loop', but the problem is that the digit 'weight' is not known until the total number of digits is determined.

Thanks,

John
_________________
John Morley
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 10, 2011 2:43 pm     Reply with quote

Somehow I feel that the 'isdigit' function would be useful for parsing if x,xx,y,yyy are numbers...
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Thu Mar 10, 2011 3:52 pm     Reply with quote

What I'd do is:

Code:
#define CR 0x0d
int1 found_A = FALSE;
int1 found_B = FALSE;
int16 first_digit = 0;
int16 second_digit = 0; // set these variable sizes to whatever you like
i = 0;

do {
   switch (MsgBuffer[i]) {
      case 'A':
         found_A = TRUE;
         found_B = FALSE;
         break;
      case 'B':
         found_B = TRUE;
         found_A = FALSE;
         break;
      default:
         if (found_A && isdigit(MsgBuffer[i]) {
            first_digit = (10 * first_digit) + (MsgBuffer[i] - 0x30);
         }
         if (found_B && isdigit(MsgBuffer[i]) {
            second_digit = (10 * second_digit) + (MsgBuffer[i] - 0x30);
         }
   }
   i++;
} while (MsgBuffer[i] != CR);


This will fail if the CR isn't in the string, but the nice thing about it is that A and B can be out of order (B comes before A) and the algorithm will still properly extract your data.

I also assumed that the number is in ascii format in the string, and that's the reason for the 0x30 offset being subtracted.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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