xQueryUser
xQueryUser

Reputation: 63

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

Upvotes: 2

Views: 496

Answers (2)

tmaj
tmaj

Reputation: 35105

One way would be to have a single array that contains a_and_b elements

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

Upvotes: 1

wablab
wablab

Reputation: 1743

In C#, this is often not the best approach, but it does answer your question specifically. You can use the unsafe and fixed keywords as follows:

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. Under the Build tab of the properties pages, check the 'Allow unsafe code' box.

Upvotes: 1

Related Questions