Home Programming (Assembly and C) Converting an Integer to ASCII

Converting an Integer to ASCII

E-mail Print PDF
( 2 Votes )
Question: How do I convert an integer value to ASCII and display it on an LCD or transmit over a UART TX?

Answer:  An integer value can be converted to ASCII string using the itoa function and then displayed on LCD using the LCD_PrString function or transmitted through UART using UART_PutString or TX8_PutString functions.  

Prototype: char *itoa(char *string, int value, int base);

Parameters:
*string - Pointer to a string where the converted ASCII result has to be stored
value -  The integer value to be converted
base - Base of conversion

Return:
The function returns the pointer of the string where the ASCII result is stored.

Code Samples


Display an integer on LCD

#include “stdlib.h”

int MyInt = -30000;    // Integer value to be converted and displayed
char AsciiString[7];   // Buffer to store the Ascii result

LCD_Position(0,0);
LCD_PrString(itoa(AsciiString, MyInt, 10));


Note: Code assumes that an LCD user module named “LCD” has been placed in the project and the LCD has been initialized


Transmit an integer value as ascii string over UART

#include “stdlib.h”

int MyInt = -30000;    // Integer value to be converted and displayed
char AsciiString[7];   // Buffer to store the Ascii result

UART_PutString(itoa(AsciiString, MyInt, 10));

Note: Code assumes that an UART user module has been placed, configured and initialized

Comments (2)
  • nisoax  - Slight lead
    I didn't discover the functions in the stdlib before that's why I'm glad to hear that there is another function to display integer values on the lcd.

    But in your example "Display an integer on LCD" you use a value which is to big for an integer. It should be 32768 at most. Otherwise you receive a false value on the lcd.
  • graaja
    Hi nisoax, Thank you for the feedback. That was a big oversight from my side. I have fixed the mistake.
    Best Regards, Ganesh
Only registered users can write comments!