需要帮助将uint8_t数组转换为NSMutableData

时间:2011-05-05 18:18:08

标签: c++ objective-c cocoa boost firebreath

我正在尝试将一些Objective-c代码(mac)移植到c ++代码(win)。但是,我有一个问题。在mac上我的数据作为NSMutableData对象进入,在Windows上它作为uint8_t数组进入。我需要将我的uint8_t数据转换为NSMutableData内的相同类型的数据。救命啊!

//on the mac
foo(NSMutableData* received)
{
   void* data = malloc([received length]);
   memcpy(data, [received mutableBytes], [received length]);

   bar(data);
}

//on windows
foo(const boost::shared_array<uint8_t>& received)
{
   void* data = ... //magic needs to happen here 

   bar(data);
}

2 个答案:

答案 0 :(得分:0)

Bar会期望获得一块内存,它将拥有并释放自己,就像你的Mac示例所示?

void *data = malloc(received_size); //shared_array can't give you the size info
memcpy(data,received.get(),received_length);
bar(data);

如果bar没有获取内存的所有权,那么您只需将数据传递给它而无需复制:

void *data = static_cast<void*>(received.get());

答案 1 :(得分:0)

呃;在Windows上没有NSMutableData对象这样的东西,所以你最终必须更具体地说明你需要它的格式。

根据apple docs,NSMutableData是字节数据的缓冲区;好吧,那就是uint8_t数组也是如此。如果它们都只假设一个数据数组,那么你可以将指针传递给块。

实际上,将NSMutableData对象转换为uint8_t指针比使用其他方式更有意义,因为这可能是你在MutableData中的内容。

希望有所帮助