Java Byte Array to Int bytesToInt(byte[] bytesSource)

Here you can find the source of bytesToInt(byte[] bytesSource)

Description

bytes To Int

License

Apache License

Declaration

public static int bytesToInt(byte[] bytesSource) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static int bytesToInt(byte[] bytesSource) {
        int num = 0;
        num |= (0xFF & bytesSource[0]);
        int i = 0;
        while (i++ < bytesSource.length - 1) {
            num = (num << 8) | (0xFF & bytesSource[i]);
        }/*  w ww .ja v  a2s  .  co  m*/
        return num;
    }

    public static int bytesToInt(byte[] bytesSource, int offset, int length) {

        if (length <= 0 || length > 4) {
            throw new IllegalArgumentException("Expected range of length is (0,4], but the input is:" + length);
        }
        int num = 0;
        num |= (0xFF & bytesSource[offset]);
        int i = 0;
        while (i++ < length - 1 && i + offset < bytesSource.length) {
            num = (num << 8) | (0xFF & bytesSource[i + offset]);
        }
        return num;
    }
}

Related

  1. bytesToInt(byte[] bytes)
  2. bytesToInt(byte[] bytes, int bitOffset, int bitCount)
  3. bytesToInt(byte[] bytes, int offset)
  4. bytesToInt(byte[] bytes, int offset, int length)
  5. bytesToInt(byte[] bytes, int start)
  6. bytesToInt(byte[] data)
  7. bytesToInt(byte[] data)
  8. bytesToInt(byte[] data)
  9. bytesToInt(byte[] value)