supermonkey
supermonkey

Reputation: 655

Transform NSData to int in Objective-c

I would like to transform NSData to int in Objective-c.

In the following code, "index2" is "15", but "value" is "0". (NSLog says "15,0".)

To be exact, "value" must be "15".

 NSString *index = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 NSString *index2 = [index stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 int value = [index2 intValue];
 NSLog(@"%@,%d",index2,value);

Could you tell me how to solve this problem?

Upvotes: 0

Views: 1384

Answers (1)

Tamil
Tamil

Reputation: 1173

If you want to simply convert nsdata to int try the following.

int theInteger;
[completeData getBytes:&theInteger length:sizeof(theInteger)];

Check this link you might need to read the NSData using a range.

Upvotes: 2

Related Questions