Wednesday, December 3, 2014

Cocos2D-X: Conversion Between String, std::string, and C String (i.e. char*)

"String" refers to the String class (previously CCString and analogous to the NSString in Objective-C). This is for Cocos2D-X 3.0 and above.

From C String to String, it's actually the standard constructor
String* cocos2dxString = String::create(“xxxxx”);

From String to C String
cocos2dxString->getCString();

From String to std::string
cocos2dxString->_string;

From std::string to String (this requires an indirect conversion it seems)
String::create(cocos2dxString->_string.c_str());

From std::string to C String
stdString.c_str();

No comments:

Post a Comment