1.
```
NSString轉(zhuǎn)化為UNICODE String:(NSString*)fname = @“Test”;char fnameStr[10];memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
與strcpy相比濒蒋,memcpy并不是遇到'\0'就結(jié)束健霹,而是一定會(huì)拷貝完n個(gè)字節(jié)
```
2
```
NSString 轉(zhuǎn)化為 char *
NSString *str = @"Test";
const char *a = [str UTF8String];
```
3
```
char * 轉(zhuǎn)化為 NSString
NSString *str=[NSString stringWithCStringencoding:NSUTF8StringEncoding];
```
4
char * 轉(zhuǎn)化 NSData
```
方法一:
char * a = (char*)malloc(sizeof(byte)*16);
NSData *data = [NSData dataWithBytes: a? length:strlen(a)];
方法二:
轉(zhuǎn)換為NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)enco
```
5
NSData 轉(zhuǎn)化 char *
```
NSData data 腾仅;
char* a=[data bytes];
```
6
NSData 轉(zhuǎn)化 NSString;
```
NSData* data;
NSString* aStr= [NSString alloc] initWithData:dataencoding:NSASCIIStringEncoding];
```
7
NSString 轉(zhuǎn)化 NSData對(duì)象
```
NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
NSString 轉(zhuǎn)化 NSURL
//NSURL *url = [NSURL URLWithString:[str? stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];
NSString *urlString=[@"http://www.google.com/search?client=safari&rls=en&q=搜索&ie=UTF-8&oe=UTF-8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL 轉(zhuǎn)化 NSString
NSURL *url=[NSURL URLWithString:urlString];
NSString *s=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *arr = [urlStr componentsSeparatedByString:@"&"];
NSURL *url = [NSURL URLWithString:[[arr objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 1; i < [arr count]; ++i ){
NSString *str = [arr objectAtIndex:i];
[_array addObject:str];
}
[_array componentsJoinedByString:@"&"];
NSData *data = [[_array componentsJoinedByString:@"&"] dataUsingEncoding: NSASCIIStringEncoding];
[request setHTTPBody:data];
// 發(fā)送請(qǐng)求并獲得服務(wù)器反饋的數(shù)據(jù)
NSData *urldata = [AESEnCDeCViewController Get:url];
// 第一種 轉(zhuǎn)換NSData數(shù)據(jù)到char*字符串
char * test = (char*)[urldata bytes];
std::string old = deaes(test);
// 第二種 轉(zhuǎn)換NSData到UTF8編碼的NSString中再轉(zhuǎn)換為char*字符串
//??? NSString *desStr = [[NSString alloc] initWithData:urldata encoding:NSUTF8StringEncoding];
//??? const char *desresult = [desStr UTF8String];
//?? ?std::string old = deaes(desresult);
// 解密字字符串到明文
NSString *oldstr = [[NSString alloc] initWithCString:old.c_str()];
```
8
很多時(shí)候軟件讀取的中文網(wǎng)頁(yè)編碼集是gb2312,所以顯示出來(lái)的是亂碼敢订。這時(shí)需要將NSString文字編碼轉(zhuǎn)換
```
1 NSURL *url = [NSURL URLWithString:urlStr];
2 NSData *data = [NSData dataWithContentsOfURL:url];
3 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
4 NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];
```