如何转换阵列<系统::字节>为char *在C ++ CLR? [英] How to convert array<System::Byte> to char* in C++ CLR?

查看:1023
本文介绍了如何转换阵列<系统::字节>为char *在C ++ CLR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我通过一个byte []从C#和C ++ CLR函数。



C ++ CLR代码:

 无效TestByteArray(阵列<系统: :字节> ^的字节数组)
{

}

C#代码:

 字节[]字节=新字节[128]; 
...
TestByteArray(字节);

在TestByteArray()函数,我需要转换的字节数组为char *,这样我可以用它在本地C ++代码。我怎么能做出这种转换


解决方案

 无效TestByteArray(阵列<系统::字节> ; ^的字节数组)
{
pin_ptr<系统::字节> p值=&放大器;的字节数组[0];
无符号字符* PBY = P;
的char * PCH = reinterpret_cast的< char *之>(PBY);

//使用它...
}


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?

解决方案

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

这篇关于如何转换阵列&lt;系统::字节&GT;为char *在C ++ CLR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆