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

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

I Know I Can Use PInvoke To Call memcmp()- Comparing Two Byte Arrays In .NET

But I want to compare only parts byte[]- with offset, and not 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 for this?

Should I use PInvoke with IntPtr? How?

Thank.

+3
source share
5 answers
+5
[DllImport("msvcrt.dll")]
private static extern unsafe int memcmp(byte* b1, byte* b2, int count);

public static unsafe int CompareBuffers(byte[] buffer1, int offset1, byte[] buffer2, int offset2, int count)
{
    fixed (byte* b1 = buffer1, b2 = buffer2)
    {
        return memcmp(b1 + offset1, b2 + offset2, count);
    }
}

.

+14

P/Invoke ++/CLI. pin_ptr < > . *, .

+2

, , , / . , for # , P/Invoking Win32. , P/Invoke , .

, #.

As with all performance issues, you must perform your own performance testing. But it sounds like you are trying to optimize your work prematurely.

+2
source

There is another way

SequenceEqual from System.Linq

 byte[] ByteArray1 = null;
 byte[] ByteArray2 = null;

 ByteArray1 = MyFunct1();
 ByteArray2 = MyFunct2();

 if (ByteArray1.SequenceEqual<byte>(ByteArray2))
 {
    MessageBox.Show("Same");
 }
 else
 {
   MessageBox.Show("Not Equal");
 }
0
source

Source: https://habr.com/ru/post/1749094/


All Articles