user530664
user530664

Reputation:

How do I convert from int to chars in C++?

i want transformation from int to char(or string) in c++.

Upvotes: 1

Views: 483

Answers (5)

Falo
Falo

Reputation: 371

Since C++11 you don't need boost, sprintf() or std::ostringstream. You can simply write:

#include <string>

std::string str = std::to_string(1077);

or

std::wstring wstr = std::to_wstring(1077);

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54128

#include <sstream>

ostringstream intStream;
int myInt(123456);

intStream << myInt;

string myIntString(intStream.str());

Upvotes: 2

Cat Plus Plus
Cat Plus Plus

Reputation: 129754

Apart from using stringstream directly, you can also use boost::lexical_cast:

std::string x = boost::lexical_cast<std::string>(42);

Upvotes: 8

Baltasarq
Baltasarq

Reputation: 12202

Well, that is not specially complex:

#include <sstream>

    std::string cnvt(int x)
    {
        std::ostringstream cnvt;

        cnvt << x;

        return cnvt.str();
    }

Hope this helps.

Upvotes: 3

Nathan Fellman
Nathan Fellman

Reputation: 127428

The simplest way to convert any basic data type to char* is with sprintf:

char mystring[MAX_SIZE];
sprintf(mystring, "%d", my_int);

Upvotes: 3

Related Questions