C# 数组转Intptr

 在c++里数据转指针是很容易的,但是在托管代码里,转起来就比较费劲了。转换方法如下:

internal static IntPtr ArrayToIntptr(byte[] source)
{
if (source == null)
return IntPtr.Zero;
unsafe
{
fixed (byte* point = source)
{
IntPtr ptr = new IntPtr(point);
return ptr;
}
}
}
internal static IntPtr ArrayToIntptr(byte[] source)
{
if (source == null)
return IntPtr.Zero;
byte[] da = source;
IntPtr ptr = Marshal.AllocHGlobal(da.Length);
Marshal.Copy(da, 0, ptr, da.Length);
return ptr;
}
如果不安全代码要用在.net里,只是在项目属性里,设置一下就行,但是要在unity里用的话就必须要加点东西了。
在unity里使用不安全代码时,要在Assetm目录下建一个smcs.rsp文件,在里边写一句-unsafe就行了,这个文件就是一个文本文件,然后改一下后缀名就行。

猜你喜欢

转载自blog.csdn.net/u011017980/article/details/70140651