Reputation: 1952
not using the C standard library do this in C++ ?
If i had to convert string to int in c++ use something like this
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
int num;
string str="2020";
(stringstream)"2020">>num;
cout<<num+2;
return 0;
}
Upvotes: 0
Views: 5227
Reputation: 363817
"Convert string
to char *
" is impossible without the C++ standard library since string
is a part of that library.
"Convert int
to char *
": I assume you mean putting the decimal representation of an int
in some buffer. This is how it can be done for unsigned
; doing the same for signed int
means you have to take a possible -
into account, and the corner case that arises from the fact that -INT_MIN
is not well-defined.
unsigned n = SOME_VALUE;
char buffer[11]; // long enough for 32-bit UINT_MAX + NUL character
char *p = buffer + sizeof(buffer);
*--p = '\0';
do {
*--p = '0' + n % 10;
n /= 10;
} while (n);
p
now points to the string representation of n
.
Upvotes: 1
Reputation: 31445
Convert string
to char *
std::vector<char> vec( str.begin(), str.end() );
vec.push_back( '\0' );
char * data = &vec[0];
Convert string
to int
std::istringstream iss(str);
int i;
if( !iss >> i )
{
std::ostringstream oss;
oss << "Invalid conversion from " << str << " to integer";
throw std::invalid_argument( oss.str() );
}
Your second answer was close to the way to do it. Note there is a boost::lexical_cast which does pretty much the same, but has the huge downside of a meaningless bad_cast exception that gives no context information and therefore renders it almost useless in my opinion.
Upvotes: 1
Reputation: 13638
#include <sstream>
#include <iostream>
#include <string>
int main (int argc, char* argv[])
{
std::string str ("123");
const char* c_str = str.c_str();
char* so_bad = const_cast<char*>(c_str);
std::stringstream ss;
ss << so_bad;
int int_value;
ss >> int_value;
std::cout << int_value;
return 0;
}
Upvotes: 1
Reputation: 10311
I don't know whether you count Boost as stdlib, but lexical_cast can cast char*s to whatever you want:
char* foo = "123"
int bar = boost::lexical_cast(foo);
And the other way round:
int foo = 123;
std::string bar = boost::lexical_cast(foo);
your_function(bar.c_str());
It's using stringstream behind the scenes, but is a lot easier to use.
Also, you can't just convert an int to char*, as the memory the char* is pointing to has to be allocated somewhere.
Upvotes: 1
Reputation: 151
I assume you mean you want a C++ solution using the C++ standard library (not using the C standard library). If so try the code below:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string stringString("2020");
cout << "String String = " << stringString << endl;
const char* charString = stringString.c_str();
cout << "Char String = " << charString << endl;
int charStringLen = stringString.size();
for (int characterIndexCtr = 0; characterIndexCtr < charStringLen; ++characterIndexCtr)
{
cout << "Character At Index " << characterIndexCtr << " = " << charString[characterIndexCtr] << endl;
}
stringstream stringStream(stringString);
int integerNumber;
stringStream >> integerNumber;
cout << "Integer = " << integerNumber << endl;
cout << "Integer + 2 = " << integerNumber + 2 << endl;
cout << "Press Enter To End Program ... ";
cin.get();
return 0;
}
Upvotes: 1