0

not using the C standard library do this in C++ ?

  1. Convert string to char*
  2. Covert int to char*

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;
}
2
  • 2
    Maybe you can clarify a bit. What are you calling a "string" here -- a basic_string<char>, or something else? You say the above is an example of converting a "string" to an int, but in fact you're converting an array of char. And the answer to number one is seemingly so trivial that you must mean something more complicated than I'm seeing. Commented Mar 18, 2011 at 13:40
  • i have a function to which i must pass string and integer data but it accepts only char*. I am not sure if i get you but let me edit the above code. Commented Mar 18, 2011 at 13:48

5 Answers 5

1
#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;
}    
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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.

2 Comments

They can and I used it for all of a few minutes until I saw how useless it was when you got an exception, not to also mention that there is no way to imbue a locale or set any other kind of formatting. I believe they have themselves seen the flaws in it and are replacing it with something much better.
definitely nice to know. the actual question was not intended to be complex and i changed it.
1

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.

Comments

1

"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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.