Here you can find the source of toInt(byte[] bytes)
Parameter | Description |
---|---|
bytes | byte array |
Parameter | Description |
---|---|
IllegalArgumentException | if length is not #SIZEOF_INT |
public static int toInt(byte[] bytes)
//package com.java2s; /**/*from w w w.j a v a2 s. c o m*/ * Copyright (C) 2016, CERN * This software is distributed under the terms of the GNU General Public * Licence version 3 (GPL Version 3), copied verbatim in the file "LICENSE". * In applying this license, CERN does not waive the privileges and immunities * granted to it by virtue of its status as Intergovernmental Organization * or submit itself to any jurisdiction. */ public class Main { public static final int SIZEOF_INT = Integer.SIZE / Byte.SIZE; /** * Converts a byte array to an int value * * @param bytes * byte array * @return the int value * @throws IllegalArgumentException * if length is not {@link #SIZEOF_INT} */ public static int toInt(byte[] bytes) { if (SIZEOF_INT > bytes.length) throw new IllegalArgumentException("length is not SIZEOF_INT"); int n = 0; for (int i = 0; i < +bytes.length; i++) { n <<= 8; n ^= bytes[i] & 0xFF; } return n; } }