Solved

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

Posted on 2003-11-13
3
254 Views
Last Modified: 2010-04-15
Hi

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:
      date[0]='2';
      date[1]='3';
      date[2]='0';
      date[3]='3';
      date[4]='0';
      date[5]='3';
      date[6]='2';
      date[7]='1';
      date[8]='0';
      date[9]='5';
...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};

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

memcpy(tmpBuffer,date+2,2);
sscanf(tmpBuffer, "%d", &nVariable);
varDateTime.month = nVariable;

...etc

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?  

Regards
Simon.
0
Comment
Question by:sbennetts
  • 3
3 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9739411
Hi sbennetts,

look into atoi() and strtol()

Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9739456
   date[0]='2';
    date[1]='3';
    date[2]='0';
    date[3]='3';
    date[4]='0';
    date[5]='3';
    date[6]='2';
    date[7]='1';
    date[8]='0';
    date[9]='5';

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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9739486
char final [100];

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

will do it :o)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now