I have been away from c for awhile and was hoping that someone could guide me on how to code the following:

I have a string for the time that is in the format hhmmss.sss (hour, minute, seconds, fraction of seconds). I would like to parse the string and store the values to integer stuct:

i.e.

int hour
int minutes
int seconds
int fractionSeconds

I am using a microntroller so I do not have time functions.

Recommended Answers

All 6 Replies

Hello,

You can do this converting by means of basic bit operations on "hhmmss.sss" for masking (e.g. '8' = 0x38 --> 0x38&0x0F --> int 8) and then composing the single int digits to short int numbers with Horner method, for example:

void pasti (char *s, struct stm *st){
  st->hh = 10*(s[0]&0x0F) + (s[1]&0x0F);
  st->mm = 10*(s[2]&0x0F) + (s[3]&0x0F);
  st->ss = 10*(s[4]&0x0F) + (s[5]&0x0F);
  st->ms = ((s[7]&0x0F)*10 + (s[8]&0x0F))*10 + (s[9]&0x0F);
}

/* where the struct is: */

struct stm {
short hh;
short mm;
short ss;
short ms;    
};

/* You may call pasti(), for example: */
char s[] = "180555.123";
stm st;
pasti(s,&st);
printf("%i   %i   %i   %i\n", st.hh, st.mm, st.ss, st.ms); 
/* 18   5   55   123 */

Hopefully that it will work.

-- tesu

This s[0]&0x0F is an unusual way to convert and ASCII digit to its binary equivalent and is not portable relying on the actual character values in the ASCII character set.

A slightly more usual way and one that is portable would be s[0] - '0' which rather than relying on the actual values of the character set relies on the stated behaviour in the C Standard and works for any character set.

You can use string functions to get the substrings for each part of the time and convert it into integer to store in your variables.
Try this out:

ptrToYourStr = yourStr;
for(i = 0; i < 3; i++)
{
     time[i] = atoi(strndup(ptrToYourStr, 2)); //Taking the substring and converting to integer
     ptrToYourStr = ptrToYourStr + 2;
}
ptrToYourStr++;   //for the . before your fractionSeconds (last part)
time[i] = atoi(ptrToYourStr);  //Rest of the string is for fractionSeconds

Have a char pointer to your string "ptrToYourStr".
I have used the time integer array just to store the time values in the loop.

Cheers

Convert the first 2 characters (0 & 1) into an integer
Convert the next 2 characters (2 & 3) into an integer
etc.

You can use string functions to get the substrings for each part of the time and convert it into integer to store in your variables.
Try this out:

ptrToYourStr = yourStr;
for(i = 0; i < 3; i++)
{
     time[i] = atoi(strndup(ptrToYourStr, 2)); //Taking the substring and converting to integer
     ptrToYourStr = ptrToYourStr + 2;
}
ptrToYourStr++;   //for the . before your fractionSeconds (last part)
time[i] = atoi(ptrToYourStr);  //Rest of the string is for fractionSeconds

Have a char pointer to your string "ptrToYourStr".
I have used the time integer array just to store the time values in the loop.

Cheers

What is the input given is 9:5:10? Your code will fail then. What Walt meant was something of this sort

while(not end of string)
{
    if(the char is not :)
    {
        time[j]+= (str[i]-'0')*power;
        power*=10;
    }
    else
    {
         power=1;
         j++;   
    }  
i++;
}

Dear abhimanipal,

I think you haven't properly read the initial post (and the subject line too), where it is clearly mentioned that the input is of the format hhmmss.sss...
So, your code will certainly fail for that input...right ???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.