Reputation: 9959
I am trying to convert an long date to a readable format as
void ConvertToDateTime(unsigned long dateTime, char *result)
{
uint8_t day = dateTime % 100;
uint8_t month = (dateTime / 100) % 100;
uint8_t year = dateTime / 10000;
sprintf(result,
"%02u/%02u/%04u",
day,
month,
year);
}
The caller method...
char *readableDateTime = (char*)"";
ConvertToDateTime(atoi(datestring), readableDateTime);
While debugging the datetime parameter is 20191112
(yyyymmdd) but the result
is
null
| empty string
| nada
if you prefer.
What am I doing wrong here?
Upvotes: 0
Views: 170
Reputation: 753785
Your code writes to a 1-byte readonly area of memory. This is more likely to crash the program than do anything useful. Use:
char readableDateTime[100] = "";
Also, an 8-bit unsigned integer can only hold values 0..255. Note that 2019 % 256 is 227. Use at least uint16_t
for year
.
Upvotes: 1