Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to convert int to string?

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
I want to display an int in an LCD display. For example,
int a=174;
Now to display it in LCD, I want to send the ascii of each digit to LCD.

If it is like ,
char buf[]="174";
Then i could easily display it in lcd by using a buf in a for loop.

So i think i could display the intiger value in a variable a if i could convert it to a string....But how? And is there any other simple method for this?8O

---------- Post added at 07:13 ---------- Previous post was at 07:10 ----------

My C knowledge is limited.
 

use itoa (int to ascii) can be found in stdlib.h
as shown below...

#include <stdlib.h>


void main (void)
{
int num = 174;
char buf [5];

while(1)
{
itoa(num,10, buf);
printf("%s\n", buf); // buf contains the converted string
}
}
 
used itoa and shows error:

Warning [357] C:\Documents and Settings\vinods\Desktop\c files\Copy (12) of new1.c; 20.9 illegal conversion of integer to pointer
Warning [358] C:\Documents and Settings\vinods\Desktop\c files\Copy (12) of new1.c; 20.17 illegal conversion of pointer to integer

---------- Post added at 08:58 ---------- Previous post was at 08:57 ----------

used itoa and shows error:

Warning [357] C:\Documents and Settings\vinods\Desktop\c files\Copy (12) of new1.c; 20.9 illegal conversion of integer to pointer
Warning [358] C:\Documents and Settings\vinods\Desktop\c files\Copy (12) of new1.c; 20.17 illegal conversion of pointer to integer

---------- Post added at 09:04 ---------- Previous post was at 08:58 ----------

i am using High Tech C 9.71a

---------- Post added at 09:06 ---------- Previous post was at 09:04 ----------

I tried to compile the below program and got error.

#include <htc.h>
#include <stdlib.h>
#define _XTAL_FREQ 20e6 // 20MHz
__CONFIG(0x3F3A);

void main (void)
{
TRISB=0;
PORTB=0;

int num = 123;
char buf [5];

while(1)
{
itoa(num,10, buf);
PORTB=buf[2];
}
}
 

the Hi-Tech version of itoa() is declared
Code:
 extern char *	itoa(char * buf, int val, int base);
so you call it
Code:
  itoa (buffer,i,10);
a problem with 'non-standard' funtions !
 
Last edited:
There is a nice example A Digital temperature meter using an LM35 temperature sensor :Embedded Lab

It uses divide and modulo to extract the integer digits and then adds 48 to convert integer to ascII.

For example integer 0 is char 0+48=48
For example integer 1 is char 1+48=49
For example integer 2 is char 2+48=50
For example integer 3 is char 3+48=51



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// convert Temp to characters
 
 if (tempinC/10000)
 
  // 48 is the decimal character code value for displaying 0 on LCD
 
  tempC[0] = tempinC/10000  + 48;
 
 else tempC[0] = ' ';
 
 tempC[1] = (tempinC/1000)%10 + 48;        // Extract tens digit
 
 tempC[2] =  (tempinC/100)%10 + 48;          // Extract ones digit
 
 // convert temp_fraction to characters
 
 tempC[4] =  (tempinC/10)%10 + 48;        // Extract tens digit



Alex
 

Just use the sprintf. Unless you are short of memory, it's the easiest solution.

Code:
#include <stdio.h>

void display_value(void)
  {
  unsigned int value = 22;
  char buffer[16];  /* size of line on display */
  
  sprintf(buffer, "Value is %d", value);  /* Same formatting options as printf */
  display_line(buffer);                   /* Your call to display function */
  }
 

I have written a function itoa(number) for Pic in C18. It's similar itoa function in c,c++.
it's change a 16 bit integer number to string
Code:
char chuoi_so_nguyen[7];

char *itoa(short int n )
{
	unsigned char d4, d3, d2, d1, d0, q;
	if (n < 0) 
	{
	chuoi_so_nguyen[0]='-';
	n = -n;
	}
	else
	{
	chuoi_so_nguyen[0]='+';
	}
	d1 = (n>>4) & 0xF;
	d2 = (n>>8) & 0xF;
	d3 = (n>>12) & 0xF;

	d0 = 6*(d3 + d2 + d1) + (n & 0xF);
	q = (d0 * 0xCD) >> 11;
	d0 = d0 - 10*q;

	d1 = q + 9*d3 + 5*d2 + d1;
	q = (d1 * 0xCD) >> 11;
	d1 = d1 - 10*q;

	d2 = q + 2*d2;
	q = (d2 * 0x1A) >> 8;
	d2 = d2 - 10*q;

	d3 = q + 4*d3;
	d4 = (d3 * 0x1A) >> 8;
	d3 = d3 - 10*d4;
	chuoi_so_nguyen[1]=d4+'0';
	chuoi_so_nguyen[2]=d3+'0';
	chuoi_so_nguyen[3]=d2+'0';
	chuoi_so_nguyen[4]=d1+'0';
	chuoi_so_nguyen[5]=d0+'0';
	int i=5;
// === remove  left zero number  =========
	while(chuoi_so_nguyen[1]=='0'&& i>1)
	{		
			int j;
			for(j=1;j<i;j++)
				{
					chuoi_so_nguyen[j]=chuoi_so_nguyen[j+1];
				}
			i--;

	}
	chuoi_so_nguyen[i+1]='\0';
	return chuoi_so_nguyen;
	
}
 
Last edited:

I want to display an int in an LCD display. For example,
int a=174;
Now to display it in LCD, I want to send the ascii of each digit to LCD.

Code:
unsigned int d1,d2,d3,temp,a=174;
temp=a%100;
d1=(a/100)+48;//converting to ascii
lcddata(d1);
d2=(temp/10)+48;
lcddata(d2);
d3=(temp%10)+48;
lcddata(d3);
 

Dear vinodstanur,

Did u solve the question? I too want to convert int value to a string. But when I did it in Hi Tech C cannot compile it even. pls reply.
 

If you are using mikroC then you can use IntToStr(); function.
 

Got your private...

You can try this function. It returns a pointer denoting the starting address of the string..
You need to define the string buffer (buf[] as global)...

Code:
char *int_to_string(unsigned long int i, unsigned char buf[])
{
	unsigned char temp;
	unsigned char s = 0,t = 0;
	while(i) {
		buf[s++] = i % 10 + '0';
		i /= 10;
	}
	buf[s] = 0;
	s-=1;
	for(;t<s;t++,s--) {
		temp = buf[s];
		buf[s]=buf[t];
		buf[t] = temp;
	}
	return buf;
}

So,
consider
char *p = "12345";
&
int_to_string(12345,buf);

here, p is equivalent to int_to_string(12345,buf);

hope U understood....
 
Last edited:

First Separate each individual digit of your number and then do ORING with 0x30.

For example
Code:
//Time to Display Data on LCD
        ones = adc_data % 10;
        adc_data = adc_data / 10;
        tens = adc_data % 10;
        hundreds = adc_data / 10;
       
        DelayMs(10);
        LCD_Second_Line();
        DelayMs(10);
        LCD_GOTOXY(2,1);
        LCD_Data((hundreds | 0x30));
        LCD_Data((tens | 0x30));
        LCD_Data((ones | 0x30));
        DelayMs(5000);

This code is taken from this link..

https://sites.google.com/site/coole...0/tutorial-list/temperature-monitoring-system

Have a look at this link...

Try this
 

get the length of int value to be converted. iterate length times and with each iteration extract one digit of the int value.

int myIntVal, LenghtOfIntValue, digit, i;
char myStrVal[];

myIntVal = 123
LenghtOfIntValue = 3
for (i = 1; i = LenghtOgIntVal; i++){
digit = extracted digit (extract with defined func or write your own digit extraction func)
//eg: digit = 3 of 123
do a
switch case digit
case 1
myStrVal[] = myStrVal + "1"
case 2
myStrVal[] = myStrVal + "2"
case 3
myStrVal[] = myStrVal + "3"
case 4...
.........................

}

use myStrVal with Lcd func to display the string.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top