Java Integer From intFrom2Bytes(byte[] bytes, int index)

Here you can find the source of intFrom2Bytes(byte[] bytes, int index)

Description

Get an unsigned integer from two bytes sampled from within a byte stream, starting at the specified index

License

Open Source License

Parameter

Parameter Description
bytes a 2-byte array
index the index offset into the byte array to sample

Return

an unsigned integer

Declaration

public static int intFrom2Bytes(byte[] bytes, int index) 

Method Source Code

//package com.java2s;
/*//  w ww.  j  a  v  a 2  s.  c o m
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Get an unsigned integer from two bytes sampled from within a byte stream,
     * starting at the specified index
     *
     * @param bytes a 2-byte array
     * @param index the index offset into the byte array to sample
     * @return an unsigned integer
     */
    public static int intFrom2Bytes(byte[] bytes, int index) {
        return ((bytes[index] << 8) + bytes[index + 1]);
    }

    /**
     * Get an unsigned integer from a two bytes
     *
     * @param bytes a two-byte array
     * @return an unsigned integer
     */
    public static int intFrom2Bytes(byte[] bytes) {
        return intFrom2Bytes(bytes, 0);
    }
}

Related

  1. intFrom(byte a, byte b, byte c, byte d)
  2. intFrom4Bytes(byte[] bytes, int index)
  3. intFromBase64(String value)
  4. intFromBigEndainByteArray(byte[] buf, int offset, int len)
  5. intFromByte(byte byteValue)