问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/25508138/transform-nsdata-to-int-in-objective-c