Transform NSData to int in Objective-c

≡放荡痞女 提交于 2020-01-16 10:03:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!