• C

Converting a 2 byte string variable to a numeric value - C


I am currently programming a hand-held device which has a keypad and want to set the date.  The date is set using a varDateTime structure as follows:
byte day

byte month
byte year
byte hour
byte minute
byte second

I have a date byte array which contains the string characters as follows:
...which I want to have a result of 23rd March 2003 at 9.05pm.  I'm trying to convert each of the two elements corresponding to the part of the structure as follows:
int nVariable;
char tmpBuffer[]={0x00,0x00,0x00};

sscanf(tmpBuffer, "%d", &nVariable);
varDateTime.day = nVariable;

sscanf(tmpBuffer, "%d", &nVariable);
varDateTime.month = nVariable;


I'm not sure if I'm using the right conversions or data types or pointers and not pointers etc.  There is a simulator that goes with the device that seems to run C++ and that seems to work, but not the device itself which runs native C.

I don't get the expected results, any ideas?  

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi sbennetts,

look into atoi() and strtol()


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

char temp[3];
temp[0] = date [0];
temp[1] = date [1];
temp[2] = '\0';
date = atoi(temp);

temp[0] = date [2];
temp[1] = date [3];
month = atoi (temp);

temp[0] = date [4];
temp[1] = date [5];
year = atoi (temp);

temp[0] = date [6];
temp[1] = date [7];
hour = atoi (temp);

temp[0] = date [8];
temp[1] = date [9];
minute = atoi (temp);

/*23rd March 2003 at 9.05pm*/
next you can translate these values into string format ...

a better way would be declare a struct tm ... fill in these values there and then use ctime() or strftime()
struct tm t;
              struct tm
                      int     tm_sec;         /* seconds */
                      int     tm_min;         /* minutes */
                      int     tm_hour;        /* hours */
                      int     tm_mday;        /* day of the month */
                      int     tm_mon;         /* month */
                      int     tm_year;        /* year */
                      int     tm_wday;        /* day of the week */
                      int     tm_yday;        /* day in the year */
                      int     tm_isdst;       /* daylight saving time */

fill in the values here

strftime (...);

look into help page of strftime for detailed format information
char final [100];

strftime ( final, "%d rd  %B %G at %l%p ", &t );
printf( "%s\n", final );

will do it :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.