Mega Code Archive

 
Categories / C# / File Stream
 

Compares up to n elements of byte array a[] against byte array b[]

public class Util {     /*      * Description: Compares up to n elements of byte array a[] against byte array b[].      * The first byte comparison is made between a[a_first] and b[b_first].  Comparisons continue      * until the null terminating byte '\0' is reached or until n bytes are compared.      * Return Value: Returns 0 if arrays are the same up to and including the null terminating byte       * or up to and including the first n bytes,      * whichever comes first.      */     public static int bytencmp(byte[] a, int a_first, byte[] b, int b_first, int n)     {         int elem;         for (elem = 0; elem < n; ++elem)         {             if ('\0' == a[a_first + elem] && '\0' == b[b_first + elem])             {                 return 0;             }             if (a[a_first + elem] < b[b_first + elem])             {                 return 1;             }             else if (a[a_first + elem] > b[b_first + elem])             {                 return -1;             }         }         return 0;     } }