Java Integer Create toInt(byte[] bytes)

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

Description

to Int

License

Apache License

Declaration

public static int toInt(byte[] bytes) 

Method Source Code

//package com.java2s;
/**/*from  w ww . java  2  s. c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 {
    public static int toInt(byte[] bytes) {
        return toInt(bytes, 0, 4);
    }

    public static int toInt(byte[] bytes, int offset, final int length) {
        final int SIZEOF_INT = 4;
        if (length != SIZEOF_INT || offset + length > bytes.length) {
            throw new RuntimeException("toInt exception. length not equals to SIZE of Int or buffer overflow");
        }
        final int ch1 = bytes[offset] & 0xff;
        final int ch2 = bytes[offset + 1] & 0xff;
        final int ch3 = bytes[offset + 2] & 0xff;
        final int ch4 = bytes[offset + 3] & 0xff;
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));

    }
}

Related

  1. toInt(byte[] buf)
  2. toInt(byte[] buf)
  3. toInt(byte[] buf, int off)
  4. toInt(byte[] buf, int pos)
  5. toInt(byte[] byteArray)
  6. toInt(byte[] bytes)
  7. toInt(byte[] bytes)
  8. toInt(byte[] bytes)
  9. toInt(byte[] bytes)