Solved

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

Posted on 2003-11-13
3
255 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

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to communicate between two pc's through vb.net code ? 4 146
Estimate porting issues. 9 118
XCode crashes while uploading to app store. 5 257
distributed computing in Python 4 113
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

929 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

13 Experts available now in Live!

Get 1:1 Help Now