Monday, May 26, 2008

C++ convert int to string

First: output

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();



Second: C++ function  itoa(int, buffer,num)

 #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;
}

No comments: