C # Use memcmp to compare byte Arrays

Source: Internet
Author: User

 

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. drawing;
Using system. Drawing. imaging;
Using system. runtime. interopservices;

 

/// <Summary>
/// Compare byte arrays with memcmp
/// </Summary>
/// <Param name = "B1"> byte array 1 </param>
/// <Param name = "B2"> byte array 2 </param>
/// <Returns> If the two arrays are the same, 0 is returned. If array 1 is smaller than array 2, a value smaller than 0 is returned. If array 1 is greater than array 2, a value greater than 0 is returned. </Returns>
Public static int memorycompare (byte [] B1, byte [] B2)
{
Intptr retval = memcmp (B1, B2, new intptr (b1.length ));
Return retval. toint32 ();
}

/// <Summary>
/// Compare byte Arrays
/// </Summary>
/// <Param name = "B1"> byte array 1 </param>
/// <Param name = "B2"> byte array 2 </param>
/// <Returns> If the two arrays are the same, 0 is returned. If array 1 is smaller than array 2, a value smaller than 0 is returned. If array 1 is greater than array 2, a value greater than 0 is returned. </Returns>
Public static int memorycompare2 (byte [] B1, byte [] B2)
{
Int result = 0;
If (b1.length! = B2.length)
Result = b1.length-b2.length;
Else
{
For (INT I = 0; I <b1.length; I ++)
{
If (b1 [I]! = B2 [I])
{
Result = (INT) (b1 [I]-B2 [I]);
Break;
}
}
}
Return result;
}

/// <Summary>
/// Memcmp API
/// </Summary>
/// <Param name = "B1"> byte array 1 </param>
/// <Param name = "B2"> byte array 2 </param>
/// <Returns> If the two arrays are the same, 0 is returned. If array 1 is smaller than array 2, a value smaller than 0 is returned. If array 1 is greater than array 2, a value greater than 0 is returned. </Returns>
[Dllimport ("msvcrt. dll")]
Private Static extern intptr memcmp (byte [] B1, byte [] B2, intptr count );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.