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 stringWithCString encoding: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:data encoding:NSASCIIStringEncoding];
7. NSString 转化 NSData对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; http://blog.sina.com.cn/s/articlelist_1256141290_14_1.html
NSString 转化 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 转化 NSString
NSURL *url=[NSURL URLWithString:urlString];
NSString *s=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//加密报文体格式:BASE64(前置证书版本号))| BASE64(RSA(报文加密密钥))| BASE64(3DES(报文原文))| BASE64(MD5(报文原文))
-(NSData*)encodeXML:(NSData*)data{
NSString *tempstr;
key = [[NSMutableString alloc] init];
[key setString:@"123456781234567812345678"];
NSMutableString *requeststr = [[NSMutableString alloc] init];
FC_Plugin_KaTool *tool = [[FC_Plugin_KaTool alloc] init];
NSString *version = @"1.0";
NSData *tempData;
tempData=[version dataUsingEncoding:NSUTF8StringEncoding];
tempData=[tool.base64 encode:tempData];
tempstr=[[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
[requeststr setString:tempstr];
[requeststr appendString:@"|"];
// NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"FCPay" ofType:@"bundle"]];
// NSString *txtPath = [bundle pathForResource:@"PublicKey" ofType:@"txt"];
NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"PublicKey" ofType:@"txt"];
NSString *publicKey = [[NSString alloc] initWithContentsOfFile:txtPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"publicKey = %@",publicKey);
NSString *ret = [RSA encryptString:key publicKey:publicKey];
tempData = [ret dataUsingEncoding:NSUTF8StringEncoding];
tempData=[tool.base64 encode:tempData];
tempstr = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
[requeststr appendString:tempstr];
[requeststr appendString:@"|"];
tempData=[tool._3Des encode:data key:key];
tempData=[tool.base64 encode:tempData];
tempstr = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
[requeststr appendString:tempstr];
[requeststr appendString:@"|"];
NSString *strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *strMd5PayPassword = [tool.md5 getMD5StringWithString:strData];
tempData=[tool.base64 encode:[strMd5PayPassword dataUsingEncoding:NSUTF8StringEncoding]];
tempstr = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
[requeststr appendString:tempstr];
// NSLog(@"requeststr------%@",requeststr);
return [requeststr dataUsingEncoding:NSUTF8StringEncoding];
}
- (void)requestWithMethod:(RequestMegModel *)model completion:(void (^)(BOOL success, id object))completion
{
NSMutableString *para = [[NSMutableString alloc] initWithCapacity:0];
for (NSString *mkey in [model.dict allKeys]) {
NSString *value = [model.dict objectForKey:mkey];
[para appendString:[NSString stringWithFormat:@"<%@>"
"%@"
"</%@>",mkey,value,mkey]];
}
NSString *str = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<FK application=\"%@\" terminalPhysicalNo=\"%@\">"
"%@"
"</FK>",model.application,model.terminalPhysicalNo,para];
NSMutableURLRequest *request = [self requestWithMethod:model andStr:str];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSInteger statusCode = [operation.response statusCode];
NSData *data = responseObject;
//解密 responseObject
FC_Plugin_KaTool *tool=[[FC_Plugin_KaTool alloc] init];
NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
str=[str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
str=[str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
NSArray *array = [str componentsSeparatedByString:@"|"];
if([array count]==3){
switch ([[array objectAtIndex:0] intValue]) {
case 1:
{
//正确
data = [[array objectAtIndex:1] dataUsingEncoding:NSUTF8StringEncoding];
data = [tool.base64 decode:data];
data = [tool._3Des decode:data key:key];
completion(YES,data);
}
break;
default:
{
//错误
data = [[array objectAtIndex:2] dataUsingEncoding:NSUTF8StringEncoding];
data = [tool.base64 decode:data];
NSString *temp_1=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
LLBTAlert(temp_1);//捕捉到的错误信息
completion(NO,nil);
}
break;
}
}else{
completion(NO,nil);
}
//未加密时,返回的数据
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(NO,error);
LLBTAlert(@"对不起,通讯失败");
NSLog(@"error = %@",error);
completion(NO,nil);
}];
[op start];
}
-(NSString*)returnUrl
{
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
NSString *tmpInfo = dictionary[@"waiWangUrl"];
return tmpInfo;
}
- (NSMutableURLRequest *)requestWithMethod:(RequestMegModel *)method andStr:(NSString *)str
{
NSString *stringUrl = [NSString stringWithFormat:@"%@/metapp/dispose/complex",[[LLBTRequest sharedInstance] returnUrl]];
// NSString *stringUrl = [NSString stringWithFormat:@"%@",kurl];
NSLog(@"stringUrl = %@",stringUrl);
NSString *encodeStr = [stringUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"encodeStr = %@",encodeStr);
NSURL *url = [NSURL URLWithString:encodeStr];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0f];
[request setHTTPMethod:@"POST"];
[request addValue: @"application/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:method.userId forHTTPHeaderField:@"userId"];
// [request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
// NSLog(@"str-----%@",str);
// NSLog(@"str-----%@",[str dataUsingEncoding:NSUTF8StringEncoding]);
NSLog(@"str--------%@",str);
NSLog(@"data--------%@",[self encodeXML:[str dataUsingEncoding:NSUTF8StringEncoding]]);
[request setHTTPBody:[self encodeXML:[str dataUsingEncoding:NSUTF8StringEncoding]]];
return request;
}