Solved

convert basic_string to int

Posted on 2001-08-15
10
1,001 Views
Last Modified: 2008-01-09
How do I convert a basic_string to an int?
0
Comment
Question by:navarros
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6389357
Use

basic_str _str = "123";
int _i;

_i = atoi ( _str.c_str());

or

#include <stdlib.h>
char* pc;
_i = strtol ( str.c_str(), &pc, 10);
0
 
LVL 86

Expert Comment

by:jkr
ID: 6389364
To elaborate:

'basic_str::c_str()' gives you access to the contents of the string as a NULL-terminated C-style string, so you can use the usual conversion functions like 'strtol()' or 'atoi()'
0
 
LVL 22

Expert Comment

by:nietod
ID: 6389372
>> basic_str::c_str()' gives you access to the
>> contents of the string as a NULL-terminated
>> C-style string,
But this has to be done with great caution.  You should familarize yourself with the limitations of this function if you intend to use it.   The code jkr published is a safe use of the function, but you have to be careful if you use it in other ways.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 6389389
>>but you have to be careful if you use it in other ways.

Absolutely - altering the contents of the 'const char*' (note the 'const'!) is an absolute no-no, even if a cast operation would allow you to remove the 'const' qualifier...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6389428
But also you have to be careful that array the pointer poitns to i not invalidated.  The pointer returned from c_str() points to an array of a (potentially) temporay nature.  The string object may delete ro alter this array whenever a non-cosntant member function of the string is called, this includes the destructor.  So for exanmple the code

string S = "ABC";
const char *Ptr = S.c_str();
S[0] = 'X'; // changes string, invalidates ptr.

cout << Ptr; // Error.  Ptr is invalid.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6389458
Well, now we're silghtly OT, so just the way I'd handle the 'potential temporary nature':

string S = "ABC";
const char *Ptr = strdup ( S.c_str());
S[0] = 'X'; // changes string, Ptr remains valid, but needs 'free()'.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6389512
I agree its off topic, I just didn't "like" the way you explained c_str() briefly and didn't mention its dangers.  Its too tempting to ose it for toher things and then run into problems.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6391904
template<class T, class E>
T StringTo(const basic_string<E>& source)
{
     basic_istringstream<E> iss(source);
     T ret;
     iss >> ret;
     return ret;
};

int main(int, char*[])
{
     int i = StringTo<int>(wstring(L"123"));
     i = StringTo<int>(string("456"));
     return 0;
}
0
 
LVL 1

Expert Comment

by:mail2yogi1
ID: 6392079
try this
#include <iostream.h>
#include <string.h>

void main()
{
char *str = "1234";
int a1;
a1=atoi(str);
cout<<a1<<endl;
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6421648
navaross, you got several solutions here as comments. Please accept one of them and close this question.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question