user27266: In Arduino code, I'm calling the getTextBounds() and print() API methods on my TFT handler object which accepts char* parameters and I have two integers holding a specific time's hour and minute part, for example:
int _hour = 7;
int _minute = 30;

I need to construct a char* variable that holds this string: 07:30.
What is the simplest way to do that?
I tried this:
char* timestr = (_hour < 10 ? '0' : '') + _hour + ':' + (_minute < 10 ? '0' : '') + _minute;

But it gives me two kind of errors:
error: empty character constant
error: invalid conversion from 'int' to 'char*' [-fpermissive]


user23841: What about using stdio.h? I know, it eats up some flash space, but:
#include <stdio.h>

void setup() {
  Serial.begin(115200);
}

void loop() {
  int _hour = 3;
  int _minute = 9;
  char buff[14]; // in worst case 6digigts per integer, separator and string terminating char

  sprintf_P(buff, PSTR("%02d:%02d"), _hour, _minute); // format string resides in flash (it needs special version of sprintf.
  // sprintf(buff, "%02d:%02d", _hour, _minute); // usual variant, but format is stored in RAM (and Flash too)

  Serial.println(buff);
  delay(5000);
}


user7508: You don't want a char *, you want a character array, i.e. you have to
allocate the memory for the string, not only for a pointer. That being
said, you can build the string one character at a time like this:
char timestr[6];
timestr[0] = '0' + _hour / 10;
timestr[1] = '0' + _hour % 10;
timestr[2] = ':';
timestr[3] = '0' + _minute / 10;
timestr[4] = '0' + _minute % 10;
timestr[5] = '\0';

Then you can use timestr just as you would use a char*.
Edit 1: If you are not too short on RAM, you could allocate the string
statically, and have the C runtime initialize the characters ':' and
'\0' at program startup. Then you just have to set the four remaining
chars on each conversion:
static char timestr[] = "--:--";  // initialized only once, with template
timestr[0] = '0' + _hour / 10;
timestr[1] = '0' + _hour % 10;
timestr[3] = '0' + _minute / 10;
timestr[4] = '0' + _minute % 10;

Edit 2: Maybe the “simplest”, in the sense “closest to what you had
in mind”, would be to use a String object:
String timestr = String() + (_hour < 10 ? "0" : "") + _hour
    + ':' + (_minute < 10 ? "0" : "") + _minute;

You could print() that String directly, or retrieve the internal
char* with the c_str() method.
I added this method only for completeness. Although simple in the source
code, it would not be my first choice: it uses more memory, it is more
CPU-intensive and – what worries me most – it uses heap allocation,
which carries the risk of memory fragmentation.

27266: <p>In Arduino code, I'm calling the <code>getTextBounds()</code> and <code>print()</code> API methods on my TFT handler object which accepts <code>char*</code> parameters and I have two integers holding a specific time's hour and minute part, for example:</p>

<pre><code>int _hour = 7;
int _minute = 30;
</code></pre>

<p>I need to construct a <code>char*</code> variable that holds this string: <code>07:30</code>.
What is the simplest way to do that?</p>

<p>I tried this:</p>

<pre><code>char* timestr = (_hour &lt; 10 ? '0' : '') + _hour + ':' + (_minute &lt; 10 ? '0' : '') + _minute;
</code></pre>

<p>But it gives me two kind of errors:</p>

<pre><code>error: empty character constant
error: invalid conversion from 'int' to 'char*' [-fpermissive]
</code></pre>
 23841: <p>What about using <code>stdio.h</code>? I know, it eats up some flash space, but:</p>

<pre><code>#include &lt;stdio.h&gt;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int _hour = 3;
  int _minute = 9;
  char buff[14]; // in worst case 6digigts per integer, separator and string terminating char

  sprintf_P(buff, PSTR("%02d:%02d"), _hour, _minute); // format string resides in flash (it needs special version of sprintf.
  // sprintf(buff, "%02d:%02d", _hour, _minute); // usual variant, but format is stored in RAM (and Flash too)

  Serial.println(buff);
  delay(5000);
}
</code></pre>
 7508: <p>You don't want a <code>char *</code>, you want a character array, i.e. you have to
allocate the memory for the string, not only for a pointer. That being
said, you can build the string one character at a time like this:</p>



<pre class="lang-c prettyprint-override"><code>char timestr[6];
timestr[0] = '0' + _hour / 10;
timestr[1] = '0' + _hour % 10;
timestr[2] = ':';
timestr[3] = '0' + _minute / 10;
timestr[4] = '0' + _minute % 10;
timestr[5] = '\0';
</code></pre>

<p>Then you can use <code>timestr</code> just as you would use a <code>char*</code>.</p>

<p><strong>Edit 1</strong>: If you are not too short on RAM, you could allocate the string
statically, and have the C runtime initialize the characters <code>':'</code> and
<code>'\0'</code> at program startup. Then you just have to set the four remaining
chars on each conversion:</p>

<pre class="lang-c prettyprint-override"><code>static char timestr[] = "--:--";  // initialized only once, with template
timestr[0] = '0' + _hour / 10;
timestr[1] = '0' + _hour % 10;
timestr[3] = '0' + _minute / 10;
timestr[4] = '0' + _minute % 10;
</code></pre>

<p><strong>Edit 2</strong>: Maybe the “simplest”, in the sense “closest to what you had
in mind”, would be to use a <code>String</code> object:</p>

<pre class="lang-c prettyprint-override"><code>String timestr = String() + (_hour &lt; 10 ? "0" : "") + _hour
    + ':' + (_minute &lt; 10 ? "0" : "") + _minute;
</code></pre>

<p>You could <code>print()</code> that String directly, or retrieve the internal
<code>char*</code> with the <code>c_str()</code> method.</p>

<p>I added this method only for completeness. Although simple in the source
code, it would not be my first choice: it uses more memory, it is more
CPU-intensive and – what worries me most – it uses heap allocation,
which carries the risk of memory fragmentation.</p>
