When doing data conversion, it is best to include the head file
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <sstream>
- USING_NS_CC;
- using namespace Std;
In Cocos2d-x, there is also a format brush: ccstring (data conversion often find her to be the middleman), then we want to convert the type,
you can first brush the start data type to ccstring and then turn it into the destination data type.This method is more convenient and practical.
//int Turn ccstringintnum=5; ccstring* Ns=ccstring::createwithformat ("%d", num);//ccstring ext. intintpx = Ns->intvalue ();//convert ccstring to int, turn to float type has floatvalue ()//====================================== //ccstring Turn CharConstChar*string= Ns->getcstring ();//Char turn ccstringCstring.format ("%s",Char*)//Char turn CStringccstring* Ns=ccstring::createwithformat ("%s",Char*);//====================================== //string Turn charConstChar*string=STD::string("STRING"). C_str ();CharTurnstringstringSChar*);//====================================== //string goto intSTD::stringS"1234");intn = std::atoi (S.c_str ());//function prototype int atoi (const char *);//int Turn stringintA=3; ccstring* Ns=ccstring::createwithformat ("%d", a);strings=ns->m_sstring;//======================================//string Turn ccstringSTD::stringstr ="123"; ccstring* Ns=ccstring::createwithformat ("%s", Str.c_str ()); Ccstring turnstringSTD::strings= ns->m_sstring;//NS is a ccstring* type//======================================//Char to intChar*buf ="1122";intn = std::atoi (BUF);//int turn charintA=3; ccstring* Ns=ccstring::createwithformat ("%d", a);
From for notes (Wiz)
Type conversion (ccstring int string char UTF-8 cross-transfer)