Skip to main content
Explorer
May 17, 2024
Solved

Issue casting an integer type to char type/string

  • May 17, 2024
  • 1 reply
  • 2133 views

Here is the method defined in the .h file:

void ILI9341_DrawText(const char* str, const uint8_t font[], uint16_t X, uint16_t Y, uint16_t color, uint16_t bgcolor);

the method defined in the .c file:

void ILI9341_DrawText(const char* str, const uint8_t font[], uint16_t X, uint16_t Y, uint16_t color, uint16_t bgcolor)

{

uint8_t charWidth; /* Width of character */

uint8_t fOffset = font[0]; /* Offset of character */

uint8_t fWidth = font[1]; /* Width of font */



while (*str)

{

ILI9341_DrawChar(*str, font, X, Y, color, bgcolor);



/* Check character width and calculate proper position */

uint8_t *tempChar = (uint8_t*)&font[((*str - 0x20) * fOffset) + 4];

charWidth = tempChar[0];



if(charWidth + 2 < fWidth)

{

/* If character width is smaller than font width */

X += (charWidth + 2);

}

else

{

X += fWidth;

}



str++;

}

}

If in the main.c file I invoke the following command:

v

The TFT LCD display, displays 99 correctly.

 

My issue is that if I want to pass an integer into the method it fails to complile.

 

A simple example would be that I declare and int, (int number =1;)

then invoke:ILI9341_DrawText(number, FONT4, 4, 2, WHITE, BLACK);

Moving from Arduino has been difficult as coding in Arduino allows for use of a simple cast.

 

Can someone advise how to get over this issue? Thanks in advance

    This topic has been closed for replies.
    Best answer by TombrownBottom

    Ok, I have worked it out myself. For the benefit of others.......

    I have added:

    char text[10];// create a string to hold the int value after conversion to string

     

    itoa(number, text, 10);//function to convert the int into a string (3rd argument 10 = base 10)

    Then the method:

    ILI9341_DrawText(text, FONT4, 4, 2, WHITE, BLACK);

    produces the correct output.

     

     

    1 reply

    TombrownBottomAuthorAnswer
    Explorer
    May 17, 2024

    Ok, I have worked it out myself. For the benefit of others.......

    I have added:

    char text[10];// create a string to hold the int value after conversion to string

     

    itoa(number, text, 10);//function to convert the int into a string (3rd argument 10 = base 10)

    Then the method:

    ILI9341_DrawText(text, FONT4, 4, 2, WHITE, BLACK);

    produces the correct output.