ofToInt

I’ve got this in my ofUtils…

  
  
int ofToInt(string value){  
    int number = atoi( value.c_str() );  
    return number;  
}  
  

From
http://www.linuxquestions.org/questions-…-nt-249305/
http://www.gamedev.net/community/forums-…–id=312790

Not sure if it is better to use atoi or strstream.

I think there are a few solutions:

  • atoi/atof
  • sscanf
  • sstream (strstream is deprecated)
  • poco

atoi/atof and sscanf are C functions, while sstream is part of the C++ STL. Poco::NumberParser has been unreliable for me.

I don’t feel like open frameworks is super into C purism, so I suggest the sstream solution.

  
  
int ofToInt(string str) {  
	istringstream stream(str);  
	int result;  
	stream >> result;  
	return result;  
}  
  
float ofToFloat(string str) {  
	istringstream stream(str);  
	float result;  
	stream >> result;  
	return result;  
}  
  

You could also write ofToBool and ofFromHex this way http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/