将NSData转换为int [英] Converting NSData to int

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

问题描述

我有一个NSData对象,长度是4个字节。这四个字节我是从另一个NSData对象中提取的,

I've an NSData object, the length is 4 bytes .These four bytes i'm extracting from another NSData object using ,

fourByteData=[completeData subdataWithRange:NSMakeRange(0, 16)];

我的第一个问题是,上述语句是否会为我提供完整数据的前四个字节。

My first question is, will the above statement provide me the first four bytes of complete data.

如果是,那么如何将所有这些字节转换为等效的int。

If Yes, then how to convert all these bytes to an equivalent int.

推荐答案

268566528是您期望的值还是您期望的528?如果正确的值是528那么字节顺序是big-endian但是cpu是little-endian,字节需要反转。

Is 268566528 the value you expect or perhaps you expect 528? If the correct value is 528 then the byte order is big-endian but the cpu is little-endian, the bytes need to be reversed.

所以,如果正确的值应该是528然后:

So, if the correct value should be 528 then:

NSData *data4 = [completeData subdataWithRange:NSMakeRange(0, 4)];
int value = CFSwapInt32BigToHost(*(int*)([data4 bytes]));

另请注意,网络标准订单是big-endian。

Also note that network standard order is big-endian.

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

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