int to string convert ???

S

skyer

Hello !

How to convert int value to string in QT or ANSI C++ library ?

please help

skyer
 
A

AngleWyrm

skyer said:
Hello !
How to convert int value to string in QT or ANSI C++ library ?
The C standard library function itoa (IntegerToAlpha) is what you're looking
for.

#include <iostream> // cout
#include <cstdlib> // itoa(), system()
using namespace std;

int main(void){ // command line unused
int myNumber = 42;
char myCharString[15]; // empty string 15 char long

cout << "value of myNumber is: " << myNumber << endl
<< "myCharString contains: " << myCharString << endl; // empty

// use itoa (IntegerToAlpha) to convert to string equivalent
itoa( myNumber, myCharString, 10 ); // the 10 is the number base
cout << "myCharString contains: " << myCharString << endl;

// use a different number base
itoa( myNumber, myCharString, 16 ); // hex
cout << "myCharString, base-16: " << myCharString << endl;

system("pause"); // wait for keypress
}
 
S

Senthilvel Samatharman

skyer said:
Hello !

How to convert int value to string in QT or ANSI C++ library ?

please help

skyer

Try using the stringstreams..
In the below code, the error handling is excluded for readability..

#include <iostream>
#include <sstream>

template<class T> void ConvertFromStringA(const char* pszStringRep,T&
retVal);
template<class T> std::string ConvertToStringA(const T& val);

template<class T> void ConvertFromStringA<T>(const char* pszStringRep,T&
retVal)
{
std::istringstream ist(pszStringRep);
ist >> retVal;
}

template <class T> std::string ConvertToStringA(const T& val)
{
std::eek:stringstream ost;
ost << val;
return ost.str();
}
 
D

Default User

AngleWyrm said:
The C standard library function itoa (IntegerToAlpha) is what you're looking
for.


There is no such standard function, though it is a common extension, and
therefore off-topic here.

For C++, use a stringstream.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top