在 Objective-c 中将 NSData 转换为 int [英] Transform NSData to int in Objective-c

查看:78
本文介绍了在 Objective-c 中将 NSData 转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Objective-c 中将 NSData 转换为 int.

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

在下面的代码中,index2"是15",而value"是0".(NSLog 说15,0".)

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

准确地说,值"必须是15".

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?

推荐答案

如果您想简单地将 nsdata 转换为 int,请尝试以下操作.

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

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

检查此链接,您可能需要使用范围读取 NSData.

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

这篇关于在 Objective-c 中将 NSData 转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆