在做数据转换时,最好包含以下头文件
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <sstream>
- USING_NS_CC;
- using namespace std;
int 转 CCstring
string s(char *);
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();
这里额外提一下,IOS在参与跨平台用到的编码格式转换
const char* char_url; const char* char_parameter;
格式刷:
NSString *endstr=[NSString stringWithFormat:@"%@?%@",char_url,char_parameter];
转换编码格式为utf-8
endstr=[endstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//解决中文编码
NSString 转 char*
const char *char_gurl = [endstr cStringUsingEncoding:NSUTF8StringEncoding];
char* 转 NSString
const char* gameObject;
NSString *char_gameObject = [[NSString alloc] initWithCString:(const char*)gameObject
encoding:NSUTF8StringEncoding];
char* 转 URL
const char* url;
NSString *char_url = [[NSString alloc] initWithCString:(const char*)url
encoding:NSUTF8StringEncoding];
NSURL* url=[NSURL URLWithString:char_url];