简体   繁体   English

从C ++到const uint8 *的C#转换

[英]C++ to C# conversion for const uint8*

is there a way to convert this: 有没有办法将其转换为:

function (const uint8* ptra, long src)
{

  const uint8* ptrb = ptra + src;
}

I've tried converting each const uint* to a byte[] but i'm not sure how to handle the ptra + src. 我试过将每个const uint *转换为byte [],但不确定如何处理ptra + src。 Thanks 谢谢

One way would be to have a single array that contains a_and_b elements 一种方法是拥有一个包含a_and_b元素的数组

void SomeMethod(byte[] array, long offsetForBPart)
{
   //TODO is array big enough?
   var b0 = array[0+offsetForBPart]; 
   var b1 = array[1+offsetForBPart]; 
   var b2 = array[2+offsetForBPart]; 
   //You get the idea;
}

In C#, this is often not the best approach, but it does answer your question specifically. 在C#中,这通常不是最好的方法,但是它确实回答了您的问题。 You can use the unsafe and fixed keywords as follows: 您可以如下使用unsafefixed关键字:

public unsafe void DoSomething(byte[] data, long src)
{
    fixed (byte* ptra = data)
    {
        byte* ptrb = ptra + src;
        // do your work... e.g.
        //   ptrb[0] = 10;
        //   ...is equivalent to...
        //   data[src] = 10;
    }
}

To use this approach, you'll have to compile your code as 'unsafe'. 要使用这种方法,您必须将代码编译为“不安全”。 If you're using Visual Studio, you can right-click the project node in Solution Explorer and select Properties. 如果使用的是Visual Studio,则可以在解决方案资源管理器中右键单击项目节点,然后选择“属性”。 Under the Build tab of the properties pages, check the 'Allow unsafe code' box. 在属性页的“构建”标签下,选中“允许不安全的代码”框。

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

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