Thanks, I got it.
> >> If I have a string such as "1.0" as a std::string, how can I convert
> >> that into a float or integer?
[quoted text clipped - 15 lines]
> > }
> Thanks, I got it.
The silly way to do it is: extract a C string from the C++ string, then
you could use a C way of doing it like atof() or sscanf(), or you can
get even sillier: convert the C string to an NSString, wrap that in an
NSScanner, and have it parse the string.
#include <iostream>
#include <Cocoa/Cocoa.h>
int main (int argc, char * const argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
std::string s("3.14159");
const char *sc = s.c_str();
NSString *nsc = [NSString stringWithUTF8String:sc];
NSScanner *scanner = [NSScanner scannerWithString:nsc];
float f;
if([scanner scanFloat:&f]){
std::cout << f << "\n";
}
[pool release];
return 0;
}
Can I Get a Word In - 24 Mar 2008 00:05 GMT
>>>> If I have a string such as "1.0" as a std::string, how can I convert
>>>> that into a float or integer?
[quoted text clipped - 37 lines]
> return 0;
> }
Thanks, but I needed to use a pure C++ solution.
Ben Artin - 24 Mar 2008 15:59 GMT
> Thanks, but I needed to use a pure C++ solution.
If you are using boost, then
#include <boost/lexical_cast.hpp>
string s;
int x = lexical_cast<int>(s);
and similarly for a float.
Ben

Signature
If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>
John C. Randolph - 24 Mar 2008 11:55 GMT
> NSString *nsc = [NSString stringWithUTF8String:sc];
> NSScanner *scanner = [NSScanner scannerWithString:nsc];
Once you have an NSString, you can just send it a -floatValue message.
No need to instantiate an NSScanner.
-jcr