如何在 CLR 中将数组<系统::字节>转换为字符*C++?

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

本文关键字:转换 gt 字符 C++ 字节 数组 lt 系统 CLR      更新时间:2023-10-16

在我的项目中,我将一个byte[]从c#传递到c++ CLR函数。

c++ CLR代码:

void TestByteArray(array<System::Byte>^ byteArray)
{
    ...
}
c#代码:

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

在TestByteArray()函数中,我需要将byteArray转换为char*,以便我可以在本机c++代码中使用它。如何进行转换?

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...
}

您正在寻找Encoding.GetChars()方法