How to transform several int's into one char array?

Hello everyone!

I have a project that has a Nextion display a mega and an RTC (DS3132) and my problem is the following:
From the RTC I get the date values and store each of them in 6 int variables called ( year, month, day, hour, minute, second) and now i want to send them to a text box in the Nextion. I know how to send them to 6 individual textboxes but I would like to do it to single one and with the following format : " DD/MM/YYYY HH/MM". So i will need to place the int values in a char array and also place the "/" (0x2F in HEX) between values and a space " " (0x20 in HEX) between the date and the hour. However, I have been unable to find a way of doing this, does anyone now how to?

Take a look at the sprintf() and snprintf() functions

2 Likes

@UKHeliBob Ok it works perfectly with sprintf()!

sprintf(dateBuffer,"(%d/%d/%d %d:%d)",day,month,year,hour,minute);

Thank you!

Should it be "01/01/2022", then you have to add the zeros.

Format (%02d/%02d/%d %02d:%02d) should do it.

Here is the full printf format: https://www.cplusplus.com/reference/cstdio/printf/.

@Koepel Thanks for that detail!

@Koepel I have beeen looking at the link to understand why you have to place a 02 before the integer, and what does it mean. If i understood correctly 02 is defining the length of the integer?

Okei, I think I got it! The structure after the % is [flags][width][.precision][length] so you placed a 0 for flags and a 2 for width.

The 02 format specifier says that the output should be 2 characters in length and it should be padded with zeroes to that length

2 Likes

thanks @UKHeliBob !

It is a minimal size, it can be longer. If the hour is 12345, then it will print "12345".

Suppose that the buffer is barely large enough, then one simple bug can cause another bug. Using snprintf and a buffer with some extra is always good.

1 Like

Hello , I am having trouble again with printing into the text box in Nextion. I am unable to print the " º " simbol from º C to print a temperature. Here is the small bit of code doing this:

  snprintf(tempBuffer,10,"%02d,%02d °C",T1,T2);
  Serial2.print(F("temp1.txt=\""));
  Serial2.print(tempBuffer);
  Serial2.print("\"");
  Serial2.print("\xff\xff\xff");

I keep getting " 30,41ºC " I guess this is because the simbol is not included in the printable ASCII caracters?

I don't think that the whole Unicode character set of a font is downloaded into the Nextion.
Does this help: Nextion - Perry Bebbington method - degree symbol

1 Like

Thank you very much again @UKHeliBob works perfectly!

snprintf(tempBuffer,10,"%02d,%02d %sC",T1,T2,"\xB0");