Java Byte Array Compare memcmp(byte[] s1, int s1Size, int s1offset, byte[] s2, int s2Size, int s2offset)

Here you can find the source of memcmp(byte[] s1, int s1Size, int s1offset, byte[] s2, int s2Size, int s2offset)

Description

Equiv of C library memcmp().

License

Open Source License

Parameter

Parameter Description
s1 a parameter
s1offset a parameter
s2 a parameter
n a parameter

Declaration

public final static int memcmp(byte[] s1, int s1Size, int s1offset, byte[] s2, int s2Size, int s2offset) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w w w.  j av  a2 s  .com
     * Equiv of C library memcmp().
     * 
     * @param s1
     * @param s1offset
     * @param s2
     * @param n
     * @return
     */
    public final static int memcmp(byte[] s1, int s1Size, int s1offset, byte[] s2, int s2Size, int s2offset) {

        int n = s1Size - s1offset;

        if (n > (s2Size - s2offset)) {
            n = s2Size - s2offset;
        }
        for (int i = 0; i < n; i++) {
            if (s1[i + s1offset] != s2[i + s2offset]) {
                return s1[i + s1offset] < s2[i + s2offset] ? -1 : 1;
            }
        }

        return 0;

    }
}

Related

  1. memcmp(byte a[], int a_off, byte b[], int b_off, int len)
  2. memcmp(byte[] a, byte[] b)
  3. memcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)
  4. memcmp(byte[] a, int aoff, int alen, byte[] b, int boff, int blen)
  5. memcmp(byte[] bytes, byte[] bytes2, int nbElem)
  6. memcmp(final byte[] a, final byte[] b, int length)
  7. memcmp(final byte[] first, final int firstStart, final byte[] second, final int secondStart, int size)
  8. memcmp(List header, String string, int size)