2

I have a simple sample of code that i want to convent a time_t to char array in order to concatenate it after, in an other array. Code:

time_t unixt;
char exm [40];


unixt = RTC.get();
Serial.println(unixt);
dtostrf(unixt, 10, 0, exm);
Serial.println(exm);

The problem that i face is that, the console is printing different the second one every time like this:

1548258220
1548258200

I tried also with unsigned long with no result either. Can someone point me what's wrong or an other way of doing this?

6

Use ultoa() instead of dtostrf(). The former is meant to serialize unsigned long integers, while the later is for floating point numbers. By using dtostrf(), you are implicitly converting unixt to a floating point number. The Arduino Mega only supports single precision floating point, so this conversion incurs some loss of precision, hence the result you see.

  • Thanks a lot! That was exactly what i needed. – billyVal Jan 23 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.