1.NSString转化为UNICODE String:(NSString*)fname = @“Test”;char fnameStr[10];memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节
2.NSString 转化为 char *NSString * str= @“Test”;const char * a =[str UTF8String];
3.char * 转化为 NSString
NSString *str=[NSString stringWithCStringencoding:NSUTF8StringEncoding];
4.char * 转化 NSData方法一:
char * a = (char*)malloc(sizeof(byte)*16);
NSData *data = [NSData dataWithBytes: a length:strlen(a)];
方法二:
转换为NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
5.NSData 转化 char *NSData data ; char* a=[data bytes];
6.NSData 转化 NSString;
NSData* data;
NSString* aStr= [NSString alloc] initWithData:dataencoding:NSASCIIStringEncoding];
7.NSString 转化 NSData对象NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
------------------------------
std::string->char
char szDst[256];
std::string strSrc = "hoge";
//const char* pszDst = strSrc.c_str();
sprintf(szDst, "%s", strSrc.c_str());
char->std::string
char szSrc[]="hoge";
std::string strDst = std::string(szSrc);
char->NSString
char szSrc[]="hoge";
NSString *nsstrDst = [NSString stringWithUTF8String: szSrc];
NSString->char
char szDst[256];
NSString *nsstrSrc = @"hgoe";
//const char* pszDst = [nsstrSrc UTF8String];
sprintf(szDst, "%s", [nsstrSrc UTF8String]);
std::string转NSString
std::string_string("hello");
NSString*str=[NSStringstringWithCString:_string.c_str()encoding:[NSStringdefaultCStringEncoding]];
NSString转std::string
NSString*nsfaceName=@"HELLO";
constchar*lpfaceName=[nsfaceName UTF8String];
std::stringstrFaceName=[nsfaceName UTF8String];