Solved

convert basic_string to int

Posted on 2001-08-15
10
999 Views
Last Modified: 2008-01-09
How do I convert a basic_string to an int?
0
Comment
Question by:navarros
  • 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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
FMX StringGrid1->Canvas->FillRect Problem 3 195
How to split this in C++ 4 117
Issues with C++ Class 19 105
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. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 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