加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x类型转换(CCstring int string char UTF-8互转)

发布时间:2020-12-14 17:24:27 所属栏目:百科 来源:网络整理
导读:在做数据转换时,最好包含以下头文件 #include iostream #include cmath #include string #include sstream USING_NS_CC; using namespace std ; 在cocos2d-x中,也有一个格式刷:CCString(数据转换常常找她做中间人),那么我们要转换类型,可先将起始数据类型

在做数据转换时,最好包含以下头文件

#include <iostream>
#include <cmath>
#include <string> 
#include <sstream>
USING_NS_CC;
using namespace std;

在cocos2d-x中,也有一个格式刷:CCString(数据转换常常找她做中间人),那么我们要转换类型,可先将起始数据类型刷成CCString然后再转成目的数据类型,这个方法比较方便且实用。

//int 转 CCstring

int num=5;
 CCString* ns=CCString::createWithFormat("%d",num);

 //CCstring 转 int
int px = ns->intValue();//将CCString 转换为int的方法,转成float类型有 floatValue()
//======================================

//CCString 转 char
const char *string= ns->getCString();

//char 转 CCString
CString.Format(”%s”,char *)//char 转cstring 
 CCString* ns=CCString::createWithFormat("%s",char *);
//======================================

//string 转 char
const char *string=std::string("STRING").c_str();
charstring
string s(char *);
//======================================

//string 转 int
std::string s("1234");
int n = std::atoi(s.c_str());//函数原型int atoi(const char *);

//int 转 string
int a=3;

CCString* ns=CCString::createWithFormat("%d",a);

string s=ns->m_sString;

//======================================

//string 转 CCString
std::string str = "123";

CCString* ns=CCString::createWithFormat("%s",str.c_str());

CCString 转 string

std::string s= ns->m_sString;//ns是一个CCString*类型

//======================================

//char 转 int

 char *buf = "1122";

 int n = std::atoi(buf);

//int 转 char

int a=3;

CCString* ns=CCString::createWithFormat("%d",a);

const char* s=ns->getCString();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读