|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
casting ints to string
Is there a way to cast an int to a string? thanks
|
|
#2
|
|||
|
|||
|
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 */ |
|
#3
|
|||
|
|||
|
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! |
|
#4
|
||||
|
||||
|
Why not use the sprintf() function
Code:
#include <stdio.h>
int main() {
int foo = 10;
char buf[33];
sprintf(buf, "%d", foo);
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > casting ints to string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|