How to convert array<System::Byte> to char* in C++ CLR?

拥有回忆 提交于 2021-02-17 19:41:44

问题


In my project, I pass a byte[] from C# to C++ CLR function.

C++ CLR code:

void TestByteArray(array<System::Byte>^ byteArray)
{
    ...
}

C# code:

byte[] bytes = new byte[128];
...
TestByteArray(bytes);

In the TestByteArray() function, I need convert byteArray to char*, so that I can used it in native C++ code. How can I do such conversion?


回答1:


void TestByteArray(array<System::Byte>^ byteArray)
{
    pin_ptr<System::Byte> p = &byteArray[0];
    unsigned char* pby = p;
    char* pch = reinterpret_cast<char*>(pby);

    // use it...
}



回答2:


You're looking for the Encoding.GetChars() Method



来源:https://stackoverflow.com/questions/7707985/how-to-convert-arraysystembyte-to-char-in-c-clr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!