English 中文(简体)
如何用斜线的两部分(被抵消)来形容?
原标题:How to call memcmp() on two parts of byte[] (with offset)?

我想有效地比较<代码>byte[]部分——因此,我理解<代码>memcmp(<>/code>。

我知道,我可以使用PInvoke打电话memcmp(-https://stackoverflow.com/questions/43289/comparing-second-arrays-in-net>。 比较www.NET的两种星体

但是,我只想比较<代码>byte [ ]——使用抵消,没有<代码>memcmp(,但因使用点数而抵销。

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

我是否应该利用C++/CLI来做到这一点?

我是否应该使用PInvoke语,因为IntPtr? 如何?

谢谢。

最佳回答

C++/C>,如果你已经使用该项目,则由它来实施。

How about (array,被抵消)?

问题回答
[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);
    }
}

您也不妨增加一些参数的验证。

不管怎样,你都应检查,抵消/计值对特定阵列有效。 在你这样做之后,我看不出为什么仅仅做“<>条码>。 C#中的休息时间要低于P/Invoking a Win32方法。 参看P/Invoke的许多间接费用,即不值得。

而且,C#总是不安全。

和所有业绩问题一样,你应当自己进行业绩测试。 但正如你试图过早地优化业绩一样,这令我感到欣慰。

C++/CLI无需P/Invoke。 Use pin ; 页: 1

还有一种办法。

经常使用系统。 Linq

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

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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签