Solved

convert basic_string to int

Posted on 2001-08-15
10
1,000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

751 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