none
Function memcmp() for C#? RRS feed

  • Question

  • Hello!

    Could anyone tell me the code for memcmp() function for C# or where to find it? I need to compare equality of byte[] arrays very fast. 

    Thanks!

    Pablo

    Sunday, November 17, 2019 10:46 PM

Answers

  • You can see  memcmp (msvcrt)
    • Marked as answer by Tigre Pablito Friday, November 29, 2019 10:50 PM
    Monday, November 18, 2019 12:05 AM
  • Hi Tigre Pablito, 

    Thank you for posting here.

    I make a simple test which use memcmp (msvcrt) to compare byte array on my side.

    Here’s the code, and you can refer to and modify it.

           [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
            private static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);
            public static int MemcmpCompare(byte[] b1, byte[] b2)
            {
                IntPtr retval = memcmp(b1, b2, new IntPtr(b1.Length));
                return retval.ToInt32();
            }
            static void Main(string[] args)
            {
                string str1 = "123";
                string str2 = "abcd";
                byte[] b1 = Encoding.ASCII.GetBytes(str1);
                byte[] b2 = Encoding.ASCII.GetBytes(str2);
                int result = MemcmpCompare(b1,b2);
                if (result == 0)
                {
                    Console.WriteLine("b1 equals b2");
                }
                else if (result > 0)
                {
                    Console.WriteLine("b1 is greater than b2");
                }
                else
                {
                    Console.WriteLine("b1 is less than b2");
                }
                
                Console.ReadLine();
            }
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 2:51 AM
    Moderator

All replies

  • You can see  memcmp (msvcrt)
    • Marked as answer by Tigre Pablito Friday, November 29, 2019 10:50 PM
    Monday, November 18, 2019 12:05 AM
  • Hi Tigre Pablito, 

    Thank you for posting here.

    I make a simple test which use memcmp (msvcrt) to compare byte array on my side.

    Here’s the code, and you can refer to and modify it.

           [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
            private static extern IntPtr memcmp(byte[] b1, byte[] b2, IntPtr count);
            public static int MemcmpCompare(byte[] b1, byte[] b2)
            {
                IntPtr retval = memcmp(b1, b2, new IntPtr(b1.Length));
                return retval.ToInt32();
            }
            static void Main(string[] args)
            {
                string str1 = "123";
                string str2 = "abcd";
                byte[] b1 = Encoding.ASCII.GetBytes(str1);
                byte[] b2 = Encoding.ASCII.GetBytes(str2);
                int result = MemcmpCompare(b1,b2);
                if (result == 0)
                {
                    Console.WriteLine("b1 equals b2");
                }
                else if (result > 0)
                {
                    Console.WriteLine("b1 is greater than b2");
                }
                else
                {
                    Console.WriteLine("b1 is less than b2");
                }
                
                Console.ReadLine();
            }
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 2:51 AM
    Moderator
  • Thank you, Castorix and Xingyu!
    Friday, November 29, 2019 10:53 PM