1

My C is rusty, so please forgive this simple problem.

This line works:

Tft.drawString("Test",200,200,2,WHITE);

But, this has compile errors:

String yStr = "Test";  
Tft.drawString(yStr,200,200,2,WHITE);  

The function in the library:

void drawString(char *string,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor);

The error is "no known conversion for argument 1 from 'String' to 'char*'".

Conclusion - I need a character array. Here's what I am trying to accomplish.

I have two integers that I would like to display on my Tft screen. For example, 225 and 250 to be displayed as "225,250".

I haven't figured out a way to build the char array for the drawString() function.

Assistance would be appreciated.


Thanks for the quick reply Ignacio.
I tried that, but the compiler still doesn't like it.

Here's how I built the string:

string stringXY ;
stringXY += xStr;
stringXY += ",";
stringXY += yStr;
Tft.drawString(stringXY.c_str(),200,200,2,RED);  

Error: invalid conversion from 'const char*' to 'char*' [-fpermissive]

I am apparently using c_str() wrong?

  • The pure c method using a char array rather than a string is to use sprintf: char tmpBuffer[64]; sprintf(tmpBuffer,"%d,%d",xVal,yVal); – Andrew Sep 30 '16 at 11:16
1

Put it in a String and then use the c_str() method to get the associated char*.

  • Thanks for the quick reply Ignacio. I tried that, but the compiler still doesn't like it. Here's how I built the string: String stringXY ; stringXY += xStr; stringXY += ","; stringXY += yStr; Tft.drawString(stringXY.c_str(),200,200,2,RED); error: invalid conversion from 'const char*' to 'char*' [-fpermissive] I am apparently using c_str() wrong? – user3573562 May 2 '16 at 3:38
  • 2
    No, that's a bug in the TFT library. There's no reason that argument should be modified, so it should be tagged const. – Ignacio Vazquez-Abrams May 2 '16 at 3:54
1

Many thanks to Ignacio.

I changed the function in the library to:

void drawString(const char *string,unsigned int poX, unsigned int poY,unsigned int size,unsigned int fgcolor);

This fixed the problem and gave me more confidence in my vague understanding of C.

1

Another option in your situation is to not use String at all (which is preferrable for reasons I won't explain here) and exclusively use plain old C strings and their associated functions:

// Define a string buffer to hold the result, ensure it is long enough
// to hold "N1,N2". An additional char must be added to terminate the string with 0
char result[15+1];
unsigned int N1 = ...;
unsigned int N2 = ...;
// Format output as desired
sprintf(result, "%u,%u", N1, N2);
// Now you can use result with drawString
Tft.drawString(result, 200, 200, 2, RED);

sprintf is a very powerful formatting function that offers you plenty of options.

Note that I took it for granted that your numbers are unsigned int; if these were normal integer (possibly negative), you should replace "%u,%u" with "%d,%d".

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.