Java ByteOrder decode(final ByteOrder aExpectedByteOrder, final byte... aBytes)

Here you can find the source of decode(final ByteOrder aExpectedByteOrder, final byte... aBytes)

Description

Convenience method to create a single value using the given byte values in a given byte order.

License

Open Source License

Parameter

Parameter Description
aExpectedByteOrder the expected byte order;
aBytes the bytes to decode into a single value, their order depends!

Return

the word in the expected byte order.

Declaration

public static long decode(final ByteOrder aExpectedByteOrder, final byte... aBytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, J.W. Janssen/*from  www.j  a  v a  2  s .c  om*/
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     J.W. Janssen - Cleanup and make API more OO-oriented.
 *******************************************************************************/

import java.nio.*;

public class Main {
    /**
     * Convenience method to create a single value using the given byte values in
     * a given byte order.
     * 
     * @param aExpectedByteOrder
     *          the expected byte order;
     * @param aBytes
     *          the bytes to decode into a single value, their order depends!
     * @return the word in the expected byte order.
     */
    public static long decode(final ByteOrder aExpectedByteOrder, final byte... aBytes) {
        final int byteCount = aBytes.length;
        final int lastByteIdx = byteCount - 1;

        long result = 0L;

        if (aExpectedByteOrder == ByteOrder.BIG_ENDIAN) {
            for (int i = 0; i < byteCount; i++) {
                result <<= 8;
                result |= (aBytes[i] & 0xFF);
            }
        } else if (aExpectedByteOrder == ByteOrder.LITTLE_ENDIAN) {
            for (int i = lastByteIdx; i >= 0; i--) {
                result <<= 8;
                result |= (aBytes[i] & 0xFF);
            }
        }

        return result;
    }
}

Related

  1. byteArrayToNumber(byte[] data, int index, int length, ByteOrder byteOrder)
  2. complementingOrder(ByteOrder order)
  3. convertBytesToBigInteger(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
  4. createWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB)
  5. get24BitInt(byte b1, byte b2, byte b3, ByteOrder order)
  6. getByteOrder(final String value)
  7. getInt(byte[] b, int start, int end, ByteOrder byteOrder)
  8. getInt(final int offset, final byte[] buffer, final ByteOrder byteOrder)