The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
casting ints to string
Discuss casting ints to string in the C Programming forum on Dev Shed. casting ints to string C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 1st, 2002, 06:25 PM
|
|
Senior Member
|
|
Join Date: Jul 2001
Posts: 42
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
casting ints to string
Is there a way to cast an int to a string? thanks
|

June 1st, 2002, 06:43 PM
|
|
Contributing User
|
|
Join Date: Apr 2002
Location: new york
Posts: 84
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
C-Style
Prototype:
char * itoa ( int value, char * buffer, int radix );
Parameters:
value
Value to be represented as a string. buffer
Buffer where to store the resulting string. radix
Numeral radix in which value has to be represented, between 2 and 36.
/* itoa example */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
char buffer [33];
printf ("Enter a number: ");
scanf ("%d",&i);
itoa (i,buffer,10);
printf ("decimal: %s\n",buffer);
itoa (i,buffer,16);
printf ("hexadecimal: %s\n",buffer);
itoa (i,buffer,2);
printf ("binary: %s\n",buffer);
return 0;
}
/*
Output:
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110
*/
|

June 1st, 2002, 07:06 PM
|
|
Senior Member
|
|
Join Date: Jul 2001
Posts: 42
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
thanks but i'm not sure how exactly i'm supposed to use this
in my class i included #include <stdlib.h>
and I have this method
Code:
string TreeNode::getElemString()
{
int i = this->elem;
char buffer [33];
itoa (i,buffer,10);
return buffer;
}
but when I try to compile I get this error:
TreeNode.cc: In method `class string TreeNode::getElemString()':
TreeNode.cc:41: implicit declaration of function `int itoa(...)'
line 41 being itoa (i,buffer,10);
thanks!
|

June 2nd, 2002, 05:29 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Why not use the sprintf() function
Code:
#include <stdio.h>
int main() {
int foo = 10;
char buf[33];
sprintf(buf, "%d", foo);
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|