Search code examples
c#.netc++-clipinvokememcmp

How to call memcmp() on two parts of byte[] (with offset)?


I want to compare parts of byte[] efficiently - so I understand memcmp() should be used.

I know I can using PInvoke to call memcmp() - Comparing two byte arrays in .NET

But, I want to compare only parts of the byte[] - using offset, and there is no memcmp() with offset since it uses pointers.

int CompareBuffers(byte[] buffer1, int offset1, byte[] buffer2, int offset2, int count)
{
  // Somehow call memcmp(&buffer1+offset1, &buffer2+offset2, count)
}

Should I use C++/CLI to do that?

Should I use PInvoke with IntPtr? How?

Thank you.


Solution

  • C++/CLI will definitely be the cleanest, but this hardly justifies adding C++/CLI to your project if you aren't already using it.

    How about Marshal.UnsafeAddrOfPinnedArrayElement(array, offset)?