How to convert intptr_t to string [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easiest way to convert int to string in C++
Does anyone know how to do that conversion?
I need to concatenate a intptr_t type to a string, and therefore need to convert it.
It's not an int, as It's a 64 bit OS.
Correct me if I'm wrong
Thanks

intptr_t is just a number. Therefore:
Easiest way to convert int to string in C++
...and others
Converting numbers to strings and strings to numbers

Simple:
std::to_string(ip);
Well, simple if you have C++11.

std::stringstream ss;
ss << ip;
ss.str();
(or if you prefer:
ss << std::hex << ip;
)

Related

How to convert a string to a signed float? [duplicate]

This question already has answers here:
std::string to float or double
(16 answers)
Closed 8 years ago.
like :
string num = "-0.25";
how can I convert it to a signed float?
C++11: std::stof(num) to convert to float; also stod for double and stold for long double.
Historically: std::strtod (or std::atof if you don't need to check for errors); or string streams.
strtod() is a good bet for C.
I have no idea if C++ has other bets.
The std::stof function should work fine.
You can use istringstream:
std::string num = "-0.25";
std::istringstream iss ( num);
float f_val = 0;
iss >> f_val;
You can convert the string to a signed float by using the function atof. Like :
float myValue = (float)atof("0.75");
Note that you should also checked if the passed value is a valid numerical value otherwise the behaviour could be unpredictable.
There is also an other solution :
string mystr ("1204");
int myint;
stringstream(mystr) >> myint;
You can use the atof function.
http://www.cplusplus.com/reference/cstdlib/atof/
For C++ you can also use std::stof
http://www.cplusplus.com/reference/string/stof/

Boost Lexical conversion (negative string ----> float) [duplicate]

This question already has answers here:
std::string to float or double
(16 answers)
Closed 9 years ago.
What would this return?
boost::lexical_cast<float>("-2");
I am not able to find a lexical_cast conversion from string to float example in the documentation.
Thanks
This:
float value = boost::lexical_cast<float>("-2");
Is basically equivalent to this:
float value;
{
std::stringstream ss;
ss << "-2";
ss >> value;
}
Of course, Boost's lexical_cast does a few other things behind the scenes, and handles errors with exceptions rather than iostream error states, but for the most part, if a conversion through a std::stringstring will work, boost::lexical_cast will work the same way.
The value of the float is, of course, -2.0f.

How to convert Integer to string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.

convert float to string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert double to string C++?
I searched in the page but i haven't found the solution.
I have a method to convert from int to string. But now i need to convert from float/double to string. Because i want to write some data in a file.
Anyone could help me ??
Thanks in advance.
http://www.daniweb.com/software-development/cpp/threads/146718
#include <sstream>
std::string Convert (float number){
std::ostringstream buff;
buff<<number;
return buff.str();
}
You could use sprintf.
You could use stringstream with << operator.
See also Convert double to string C++?, How do I convert a double into a string in C++?, Convert double to string using boost::lexical_cast in C++?, Converting Double to String in C++, etc.
Can you not use the standard (C) function sprintf/fprintf etc?
This can help you : Convert double to string C++?
Is your file written using IOStreams? If so, just do this:
stream << number;
If not, and you really need a string, you can use an ostringstream for this. Boost's lexical_cast wraps the string streams in an easy-to-use fashion.

C++: how to add int to current string in C++? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ concatenate string and int
Hi,
In C# I can write like this:
int i = 0;
string text = "out.jpg";
while(true)
{
i++;
Object.write(i+text, stream);
}
But this is not true for C++. the problem is at: i + default.
How could I fix this in C++?
Thanks in advance. Your help is much appreciated!
You could use a stringstream...
std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
default is a keyword in C++. You can't have string default in C++. And I don't see what you are trying to achieve. Please clarify
take a look at stringstreams or boost.format http://www.boost.org/doc/libs/1_38_0/libs/format/doc/format.html
boost::format("%1%%2%") % i % default_;
default is a reserved keyword. Change the variable name to defaultStr or similar, and everything should work fine.