Java ByteBuffer to Int getUnsignedInt16LSBMSB(ByteBuffer byteBuffer)

Here you can find the source of getUnsignedInt16LSBMSB(ByteBuffer byteBuffer)

Description

get Unsigned Int LSBMSB

License

CDDL license

Declaration

public static int getUnsignedInt16LSBMSB(ByteBuffer byteBuffer) 

Method Source Code


//package com.java2s;
/*//from   www  .  j  a  v a  2 s . c  o m
 *         COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Notice
 *
 * The contents of this file are subject to the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL)
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.opensource.org/licenses/cddl1.txt
 *
 * The Original Code is Drombler.org. The Initial Developer of the
 * Original Code is Florian Brunner (Sourceforge.net user: puce).
 * Copyright 2014 Drombler.org. All Rights Reserved.
 *
 * Contributor(s): .
 */

import java.nio.ByteBuffer;

public class Main {
    private static final short MAX_UNSIGNED_BYTE = 0xFF;
    private static final int NUM_INTEGER_BITS = 4;
    private static final int NUM_SHORT_BITS = 2;

    public static int getUnsignedInt16LSBMSB(ByteBuffer byteBuffer) {
        byte[] bytes = getBytes(byteBuffer, 2 * NUM_SHORT_BITS);
        int value = 0;
        for (int i = 0; i < NUM_SHORT_BITS; i++) {
            value += getUnsignedByte(bytes[i]) << (NUM_INTEGER_BITS * i);
        }
        return value;
    }

    private static byte[] getBytes(ByteBuffer byteBuffer, int length) {
        byte[] dst = new byte[length];
        byteBuffer.get(dst);
        return dst;
    }

    public static short getUnsignedByte(byte value) {
        return (short) (value & MAX_UNSIGNED_BYTE);
    }
}

Related

  1. getUnsignedByte(ByteBuffer buffer)
  2. getUnsignedInt(ByteBuffer bb)
  3. getUnsignedInt(ByteBuffer buffer)
  4. getUnsignedInt(ByteBuffer buffer)
  5. getUnsignedInt(ByteBuffer buffer)
  6. getUnsignedLong(ByteBuffer buf)
  7. getUnsignedLong(final ByteBuffer src)
  8. getUnsignedShort(ByteBuffer bb)
  9. getUnsignedShort(ByteBuffer bb)