Here you can find the source of decode(final ByteOrder aExpectedByteOrder, final byte... aBytes)
Parameter | Description |
---|---|
aExpectedByteOrder | the expected byte order; |
aBytes | the bytes to decode into a single value, their order depends! |
public static long decode(final ByteOrder aExpectedByteOrder, final byte... aBytes)
//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; } }