Java Byte Array Compare memcmp(final byte[] a, final byte[] b, int length)

Here you can find the source of memcmp(final byte[] a, final byte[] b, int length)

Description

memcmp

License

Open Source License

Declaration

public static int memcmp(final byte[] a, final byte[] b, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int memcmp(final byte[] a, final byte[] b) {
        final int length = Math.min(a.length, b.length);
        if (a == b) { // Do this after accessing a.length and b.length
            return 0; // in order to NPE if either a or b is null.
        }//from  w  w w .  ja  v a  2 s . co m
        for (int i = 0; i < length; i++) {
            if (a[i] != b[i]) {
                return (a[i] & 0xFF) - (b[i] & 0xFF);
            }
        }
        return a.length - b.length;
    }

    public static int memcmp(final byte[] a, final byte[] b, int length) {
        for (int i = 0; i < length; i++) {
            if (a[i] != b[i]) {
                return (a[i] & 0xFF) - (b[i] & 0xFF);
            }
        }
        return 0;
    }

    public static int memcmp(final byte[] a, final byte[] b, int off, int length) {
        for (int i = 0; i < length; i++) {
            if (a[i] != b[i + off]) {
                return (a[i] & 0xFF) - (b[i + off] & 0xFF);
            }
        }
        return 0;
    }
}

Related

  1. memcmp(byte[] a, byte[] b)
  2. memcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)
  3. memcmp(byte[] a, int aoff, int alen, byte[] b, int boff, int blen)
  4. memcmp(byte[] bytes, byte[] bytes2, int nbElem)
  5. memcmp(byte[] s1, int s1Size, int s1offset, byte[] s2, int s2Size, int s2offset)
  6. memcmp(final byte[] first, final int firstStart, final byte[] second, final int secondStart, int size)
  7. memcmp(List header, String string, int size)