Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-11-13
3
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use conditional statements in the C programming language.

730 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