Type conversion (ccstring int string char UTF-8 cross-transfer)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.