Java ByteOrder createWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB)

Here you can find the source of createWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB)

Description

Creates a (16-bit) word value with the correct byte order.

License

Open Source License

Parameter

Parameter Description
aMSB the most significant byte;
aLSB the least significant byte.

Return

the 16-bit combination of both given bytes in the order of endianness.

Declaration

public static int createWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011, J.W. Janssen/*from ww w .j  av a2 s . co  m*/
 * 
 * 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 {
    /**
     * Creates a (16-bit) word value with the correct byte order.
     * 
     * @param aMSB
     *          the most significant byte;
     * @param aLSB
     *          the least significant byte.
     * @return the 16-bit combination of both given bytes in the order of
     *         endianness.
     */
    public static int createWord(final ByteOrder aByteOrder, final int aMSB, final int aLSB) {
        if (aByteOrder == ByteOrder.BIG_ENDIAN) {
            return ((aMSB << 8) & 0xFF00) | (aLSB & 0x00FF);
        }

        return ((aLSB << 8) & 0xFF00) | (aMSB & 0x00FF);
    }

    /**
     * Creates a (16-bit) word value with the correct byte order.
     * 
     * @param aMSB
     *          the most significant byte;
     * @param aLSB
     *          the least significant byte.
     * @return the 16-bit combination of both given bytes in the order of
     *         endianness.
     */
    public static int createWord(final int aMSB, final int aLSB) {
        return createWord(ByteOrder.nativeOrder(), aMSB, aLSB);
    }
}

Related

  1. byteArrayToNumber(byte[] data, int index, int length, ByteOrder byteOrder)
  2. complementingOrder(ByteOrder order)
  3. convertBytesToBigInteger(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
  4. decode(final ByteOrder aExpectedByteOrder, final byte... aBytes)
  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)