2011-05-05 32 views
3

我想将一些Objective-C代码(mac)移植到C++代码(win)。但是,我有一个问题。在Mac上,我的数据以NSMutableData对象的形式出现,在Windows上以uint8_t数组的形式出现。我需要将我的uint8_t数据转换为NSMutableData中相同类型的数据。帮帮我!需要帮助将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); 
} 

回答

0

是否酒吧预计会得到一个内存块,它将拥有和释放自己作为你的Mac示例指示?

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

如果栏不占用内存的所有权,那么你可以将数据传递给它不进行复制:

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

恩;在Windows上不存在NSMutableData对象这样的事情,所以您最终必须更加具体地了解您需要的格式。

根据apple文档NSMutableData是字节数据的缓冲区;那么,这也是一个uint8_t数组。如果它们都只是假设一组数据,那么你只需将指针传递给块即可。

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

希望可以帮到