About int and char thing

Hi all.
The TFT_eSPI.h has: drawString(const char *string, int32_t x, int32_t y, uint8_t font), // Draw string using specified font number
and I have int count = 0; how to drawString my count instead of string?

count is any natural number.

like: drawString(count, int32_t x, int32_t y, uint8_t font), ?
Thanks
Adam

The function is expecting an ascii string stored in a char array. This is the normal way to handle strings in C.

There are a number of functions that can help. For a simple int variable itoa is probably appropriate. If you want to build more complex strings, then sprintf is your friend.

int count = 1;
char string[5];  //  needs to be big enough to hold all digits and a null
sprintf(string, "%d", count);
drawString(string, x, y, font);

EDIT: sprintf is better.

1 Like

Great!
It works!
Thank you Delta_G.

did you mean snprintf() ?

I find strtol() (and its siblings) to be more adequate (and uses up less flash) when you have only one number to read.

What's the one that goes the other way? I don't really like itoa() because it's not standard. That's why I switched the answer to use sprintf.

oops sorry - coffee time ! indeed you want to go from int to ascii.

that being said, itoa() is part of the standard lib but not ANSI-C and is not part of C++. the arduino environment and our compiler supports it.

I thought so. But as I went looking for a link to give the OP about itoa I kept finding stuff that said to use sprintf instead for that reason. So I changed the answer. I didn't want to give an answer and then provide a link that said to do something else.

yes as they say in the doc

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

and so they recommend sprintf() as the portable option (but this comes with consequences I think on the code size on our small MCUs)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.