Maker Pro
Maker Pro

Converting int to string - C Programming

Sadlercomfort

Ash
Feb 9, 2013
424
Joined
Feb 9, 2013
Messages
424
Hi Guys,

How can I convert an int to a string using C programming Hi-Tech Compiler?

I am using an ADC pin on a pic to measure a voltage, but need to convert the value measured to a string before I can display it on an LCD.
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
#include <stdio.h>

char *itoa(char *buffer, int i)
{
unsigned int n;
unsigned int negate = 0;
int c = 6;

if (i < 0)
{
negate=1;
n = -i;
}
else if (i == 0)
{
buffer[0] = '0';
buffer[1] = 0;
return buffer;
}
else
{
n = i;
}
buffer[c--] = 0;
do
{
buffer[c--] = (n % 10) + '0';
n = n / 10;
} while (n);
if (negate)
{
buffer[c--] = '-';
}
return &buffer[c+1];
}


try this: I copied this from Microchip forum.

http://www.microchip.com/forums/m263125.aspx
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
The itoa() function should be in the library. No need to include the source.

Bob
 

Sadlercomfort

Ash
Feb 9, 2013
424
Joined
Feb 9, 2013
Messages
424
Thought I'd share the solution, here I've created an integer and converted it to a string. Very simple.

Code:
//Hi-Tech C Compiler
//Integer to String using itoa function
//Displayed on LCD

#include <htc.h>
#include <stdlib.h>
#include "LCD 4-BIT.h"
#define _XTAL_FREQ 4000000  
__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF );  

unsigned int value = 32;           //Define integer value

void main(void)
{
TRISB = 0b00000000;                //Set LCD port as output
LCD_init();                        //Initialise LCD

    while(1)
    {
    char bufr[2];                 //Create array to store 2 ASCII chars    
    itoa(bufr,value,10);           //Convert value to string and place in bufr (BASE 10)

    LCD_cmd(LCD_LINE1);            //Set LCD to first line
    LCD_string(bufr);              //Display bufr string
    }
}
 
Last edited:

NorthGuy

Mar 24, 2016
53
Joined
Mar 24, 2016
Messages
53
You also need a space in the buffer to hold terminating NULL character.
 

DatLe

May 31, 2016
15
Joined
May 31, 2016
Messages
15
char bufr[2];
With this bufr, you just convert number with only one character.
Ex: integer 5 --> char , bufr[0]='5'; bufr[1]=/NULL;
With integer 15 -->char, bufr[0]='1', bufr[1]='5', bufr[2]=/NUL --> over flow -->leak memory --> one of the most bug. Some time, this bug is can not debug (very dangerous)
 

Sadlercomfort

Ash
Feb 9, 2013
424
Joined
Feb 9, 2013
Messages
424
My null character is bufr[2] though right?

unsigned int value = 32;

bufr[0]='3'
bufr[1]='2'
bufr[2]=/0
 

NorthGuy

Mar 24, 2016
53
Joined
Mar 24, 2016
Messages
53
I've tested this code and it works fine.

Doesn't matter. It is busting memory somewhere. May not affect anything if the memory you're busting is unused. Then, when your program grows, it may stay harmless. Or it may byte you. Symptoms are simple. Your program crashes or behaves in an unpredictable way. Since at that moment you're working on something totally different, it takes lots of work to trace it to a buffer overrun in some piece of code which you wrote years ago.
 

Sadlercomfort

Ash
Feb 9, 2013
424
Joined
Feb 9, 2013
Messages
424
Yeah I guess your write but again isn't my null character bufr[2]? So my code is right?

unsigned int value = 32;

bufr[0]='3'
bufr[1]='2'
bufr[2]=/0
 

NorthGuy

Mar 24, 2016
53
Joined
Mar 24, 2016
Messages
53
Yeah I guess your write but again isn't my null character bufr[2]? So my code is right?

unsigned int value = 32;

bufr[0]='3'
bufr[1]='2'
bufr[2]=/0

But you declared your array as containing only 2 elements:

char bufr[2];

These elements are bufr[0] and bufr[1]. bufr[2] is outside of reserved memory.
 

Sadlercomfort

Ash
Feb 9, 2013
424
Joined
Feb 9, 2013
Messages
424
Isn't that three elements if i declared char bufr[2];

1. bufr[0]
2. bufr[1]
3. bufr[2]
 
Top