sbennetts
asked on
Converting a 2 byte string variable to a numeric value - C
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,0x0 0};
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.
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,0x0
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char final [100];
strftime ( final, "%d rd %B %G at %l%p ", &t );
printf( "%s\n", final );
will do it :o)
strftime ( final, "%d rd %B %G at %l%p ", &t );
printf( "%s\n", final );
will do it :o)
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