我有一个函数可以将本机代码转换为托管代码,以实现C ++ / CLI中的无符号缩写,但是我的方法需要调用两次复制。还有其他方法可以使此过程更快吗?

//input
unsigned short* m_rawDataPtr;
//output
array<uns16>^ m_rawData;

...
int size = data.GeSize();
array<short>^ dataSigned = gcnew array<short>( size / 2);
m_rawData = gcnew array<unsigned short>((int)size / 2);

Marshal::Copy(IntPtr((void*)(m_rawDataPtr),dataSigned, 0, dataSigned->Length);
System::Buffer::BlockCopy(dataSigned, 0, m_rawData, 0, (Int32)(size));


谢谢,

最佳答案

您可以尝试这样的事情:

//input
unsigned short* m_rawDataPtr;
//output
array<unsigned short>^ m_rawData;

cli::pin_ptr<unsigned short> pinnedManaged = &m_rawData[0];
std::memcpy(pinnedManaged , m_rawDataPtr, arrayLength * sizeof(unsigned short));

关于c++ - 在C++/CLI中从 native 转换为无符号短的管理的最快方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50975868/

10-12 20:33