Java Integer Create toInt(byte[] src)

Here you can find the source of toInt(byte[] src)

Description

Converts a given byte array to an integer.

License

Apache License

Parameter

Parameter Description
src A big-endian representation of an integer

Return

The integer value represented by the source array

Declaration

public static int toInt(byte[] src) 

Method Source Code

//package com.java2s;
/*/* ww  w  .  j  a v  a2s .  co  m*/
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Converts a given byte array to an integer. Reads the bytes in big-endian
     * order.
     *
     * This method does not check the source array length.
     *
     * @param src A big-endian representation of an integer
     * @return The integer value represented by the source array
     */
    public static int toInt(byte[] src) {
        return readInt(src, 0);
    }

    /**
     * Reads 4 big-endian ordered bytes from a given offset in an array and
     * returns an integer representation.
     *
     * This method does not check the source array length.
     *
     * @param src The source array to read bytes from
     * @param offset The offset to start reading bytes from.
     * @return The integer value represented by the source array from the offset
     */
    static int readInt(byte[] src, int offset) {
        int output = 0;
        output |= (src[offset++] & 0xFF) << 24;
        output |= (src[offset++] & 0xFF) << 16;
        output |= (src[offset++] & 0xFF) << 8;
        output |= (src[offset++] & 0xFF);
        return output;
    }
}

Related

  1. toInt(byte[] n)
  2. toInt(byte[] readBuffer, int o)
  3. toInt(byte[] res)
  4. toInt(byte[] si)
  5. toInt(byte[] src)
  6. toInt(byte[] src, int srcPos)
  7. toInt(byte[] value)
  8. toInt(byte[] value)
  9. toInt(char msc, char lsc)