Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-13
3
Medium Priority
?
261 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

926 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