Solved

convert basic_string to int

Posted on 2001-08-15
10
995 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
navaross, you got several solutions here as comments. Please accept one of them and close this question.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now