Java Byte Array to Int bytes2int(byte[] bytes)

Here you can find the source of bytes2int(byte[] bytes)

Description

Converts Byte Array into Integer value

License

Open Source License

Parameter

Parameter Description
bytes Array that will be converted

Return

Integer value of specified Array

Declaration

public static int bytes2int(byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  . j a  v a  2  s .c o  m*/
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  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
 */

public class Main {
    /**
     * Converts Byte Array into Integer value
     *
     * @param bytes Array that will be converted
     * @return Integer value of specified Array
     */
    public static int bytes2int(byte[] bytes) {
        if (bytes == null) {
            return 0;
        }
        switch (bytes.length) {
        case 1:
            return bytes[0] & 0xFF;
        case 2:
            return (bytes[0] & 0xFF) << 8 | bytes[1] & 0xFF;
        case 3:
            return (bytes[0] & 0xFF) << 16 | (bytes[1] & 0xFF) << 8 | bytes[2] & 0xFF;
        case 4:
            return (bytes[0] & 0xFF) << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | bytes[3] & 0xFF;
        default:
            return 0;
        }
    }
}

Related

  1. bytes2int(byte[] b)
  2. bytes2Int(byte[] b, int start, int len)
  3. bytes2int(byte[] buf)
  4. bytes2int(byte[] bytes)
  5. bytes2Int(byte[] bytes)
  6. bytes2int(byte[] bytes)
  7. bytes2int(byte[] bytes)
  8. bytes2int(byte[] bytes, int begin)
  9. bytes2Int(byte[] bytes, int defaultValue)