Java Unsigned Number Create unsignedArrayCompareLex(byte[] a, byte[] b)

Here you can find the source of unsignedArrayCompareLex(byte[] a, byte[] b)

Description

unsigned Array Compare Lex

License

Open Source License

Declaration

public static int unsignedArrayCompareLex(byte[] a, byte[] b) 

Method Source Code

//package com.java2s;
/*-//from  ww w .j a v a 2  s  .  c o  m
 * Copyright (C) 2006-2008 Erik Larsson
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

public class Main {
    public static int unsignedArrayCompareLex(byte[] a, byte[] b) {
        return unsignedArrayCompareLex(a, 0, a.length, b, 0, b.length);
    }

    public static int unsignedArrayCompareLex(byte[] a, int aoff, int alen, byte[] b, int boff, int blen) {
        int compareLen = alen < blen ? alen : blen; // equiv. Math.min
        for (int i = 0; i < compareLen; ++i) {
            int curA = a[aoff + i] & 0xFF;
            int curB = b[boff + i] & 0xFF;
            if (curA != curB)
                return curA - curB;
        }
        return alen - blen; // The shortest array gets higher priority
    }

    public static int unsignedArrayCompareLex(char[] a, char[] b) {
        return unsignedArrayCompareLex(a, 0, a.length, b, 0, b.length);
    }

    public static int unsignedArrayCompareLex(char[] a, int aoff, int alen, char[] b, int boff, int blen) {
        int compareLen = alen < blen ? alen : blen; // equiv. Math.min
        for (int i = 0; i < compareLen; ++i) {
            int curA = a[aoff + i] & 0xFFFF; // Unsigned char values represented as int
            int curB = b[boff + i] & 0xFFFF;
            if (curA != curB)
                return curA - curB;
        }
        return alen - blen; // The shortest array gets higher priority
    }
}

Related

  1. unsigned32ToInt(byte[] bytes)
  2. unsigned4BytesToInt(byte[] buf, int pos)
  3. unsigned8(byte b)
  4. unsigned_byte(byte b)
  5. unsignedAddOverflow(int operand1, int operand2)
  6. unsignedArrayCompareLex(char[] a, char[] b)
  7. unsignedBinarySearch(final short[] array, final int begin, final int end, final short k)
  8. unsignedByte(int b)
  9. unsignedByteFromInt(int i)