在NSData的值中复制NSString的值而不更改值的内容

时间:2014-03-28 10:34:28

标签: objective-c nsstring

我从服务器获得了NSString变量的响应,我需要使用此值NSString来解密3DES,但必须是NSData。我尝试了从NSStringNSData的转换,但由于它更改了值的内容,因此无法正常工作。这是我的代码:

outputStream write:[encrypted bytes] maxLength:[encrypted length]];
uint8_t buffer[1024];
int len = [inputStream read:buffer maxLength:sizeof(buffer)];
myString = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

/* Here I get the response from server as NSString, and this exact value must be decrypted */

NSLog(@"response of the server in nsstring %@", mystring);
NSData *responseserv= [mystring dataUsingEncoding:NSUTF8StringEncoding];

/* I convert it to NSData but the content of the value has changed*/

NSLog(@" response of the server in nsdata: %@", responseserv);

NSString *decrypted = [[self class] tripleDesDecryptData:responseserv key:@"Xm9d7@4Ru&2YaP&?14niO$f%" error:nil];

1 个答案:

答案 0 :(得分:0)

尝试这样做..

   NSString* str = @"teststring";
   NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

这是将NSString转换为NSData的方法