简体   繁体   English

将UInt8 C ++值转换为C#字节数组

[英]Converting UInt8 C++ values to C# byte array

I have a 1-dimensional C++ array containing UInt8 values - I would like to convert those values and place them into a 2D C# byte array. 我有一个包含UInt8值的一维C ++数组-我想转换这些值并将它们放入2D C# byte数组中。 Currently, my code is giving me incorrect values. 目前,我的代码给了我不正确的值。 Here is my code: 这是我的代码:

IntPtr ptr = GetDataFromCPPSide();    

byte[,] data = new byte[dimension1, dimension2];
for(int i = 0; i < dimension1; i ++)
{
    for(int j = 0; j < dimension2; j++)
    {
        data[i, j] = Marshal.ReadByte((IntPtr)(ptr.ToInt64() + j + i * dimension2) * 1));
    }
}

Right now this only converts some values correctly. 现在,这只能正确转换一些值。 I'm sure it has something to do with the way I'm converting the value in the for loop. 我确定它与我在for循环中转换值的方式for I've placed a check on the C++ side and the values are being returned properly. 我已经在C ++方面进行了检查,并且值已正确返回。 Any ideas? 有任何想法吗?

Using: 使用:

pingdata[i, j] = (byte)Marshal.ReadByte((IntPtr)(ptr.ToInt64() + (i * d2 + j)));

fixed the issue. 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM