将NSData复制到int32_t变量中 [英] copy NSData into int32_t variable

查看:63
本文介绍了将NSData复制到int32_t变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的NSDictionary,里面满是NSData类型的条目.我有几个条目必须是int32_t类型,但是我不确定100%如何跨NSDictionary的条目复制数据.

I have a big NSDictionary full of entries that are all of type NSData. I have several entries that need to be of type int32_t however I am not 100% sure how to copy the data in the entries of the NSDictionary across..

是否和以下操作一样简单?

is it as simple as doing the following -

.h

.h

//..
int32_t myint;
}
@property (assign) int32_t myint;
//..

.m

.m

//..
@synthesize cardID;
//..
- (void)assignSearchData:(NSData*)searchData
{
myint = [searchData objectForKey:@"IntKey"];
}
//..

还是我的方法中需要某种类型的数据转换?

or do I need some type of data conversion inside my method?

还有一个简短的问题,我什至正确地声明了int32_t吗?我已经在文档中和此处寻找了一个示例,但正在努力寻找一个示例.

and a quick side question, have I even declared the int32_t correctly? I have looked for an example in the docs and on here but am struggling to find one.

推荐答案

好,您可以直接访问数据对象中的原始字节.

Well, you can access the raw bytes in the data object directly.

void const *dataPtr = [data bytes];

现在您有了指向原始内存的指针,您可以按任意方式复制它(这些规则适用于任何数据传输,而不仅限于iOS).如果需要考虑对齐边界,则需要使用memcpy.

Now that you have a pointer to raw memory, you can copy it any way you want (these rules apply to any data transfer, not just iOS). If you need to consider alignment boundaries, you need to use memcpy.

int32_t myInt;
memcpy(&myInt, dataPtr);

否则,如果在允许跨对齐边界进行整数操作的体系结构上……

Otherwise, if on an architecture that allows integer manipulation across alignment boundaries...

int32_t myInt = *(int32_t const *)dataPtr;

现在,ARM支持跨对齐边界的访问,但是速度慢得多.我尚未进行性能比较,但是您不会继续使用mal-aligned指针,因此它可能比memcpy函数调用更好(不过,老实说,这可能对您来说是过多的性能考虑).

Now, ARM supports access across alignment boundaries, but it's much slower. I have not done a performance comparison, but you are not continuing to use the mal-alignged pointer, so it may be better than the memcpy function call (though, to be honest, that is probably way too much performance consideration for you).

最大的顾虑是数据的字节顺序.如果您提供了它,那么您可以做任何您想做的事,但是您应该更喜欢一种标准.

The biggest concern is byte-order of the data. If it's provided by you, then do whatever you want, but you should prefer one standard.

如果来自第三方,则可能是网络字节顺序(又名big-endian).您可能需要转换为主机字节序表示.幸运的是,这与 hton ntoh 以及他们的朋友很直接.

If it's coming from a third party, it's probably in network byte order (aka big-endian). You may need to convert to your host endian representation. Fortunately, that's straight forward with hton and ntoh and their friends.

FWIW,英特尔是小端,网络字节顺序是大端,现代Mac和iOS设备是小端,较旧的Mac是大端.

FWIW, Intel is little-endian, and network-byte-order is big-endian, modern Macs and iOS devices are little-endian, older Macs are big-endian.

// Convert from network order to host order.
// Does the right thing wherever your code is running
myInt = ntohl(myInt);

简而言之,要么...

In short, either...

int32_t myInt = ntohl(*(int32_t const *)[data bytes]);

int32_t myInt;
memcpy(&myInt, [data bytes);
myInt = ntohl(myInt);

因此,数据必须以某种方式到达那里.是相反的...

So, the data has to get in there somehow. It's, the inverse...

int32_t myInt = 42;
myInt = htonl(myInt);
NSData *data = [NSData dataWithBytesNoCopy:&myInt length:sizeof(myInt) freeWhenDone:NO];

当然,请使用正确的数据初始值设定项...那将只使用堆栈上的那些原始字节,因此最好不要在堆栈展开后使用它.

Of course, use the right Data initializer... that one will just use those raw bytes on the stack, so you better not use it after the stack unwinds.

您不必担心发送数据的对齐方式,除非您向接收方保证数据将与某个边界对齐.

You don't have to worry about alignment on the data you send, unless you are guaranteeing the receiver that the data will be aligned to some boundary.

这篇关于将NSData复制到int32_t变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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