Android Byte Array to Int Convert toInteger(byte[] b, int pos)

Here you can find the source of toInteger(byte[] b, int pos)

Description

to Integer

Declaration

public static int toInteger(byte[] b, int pos) 

Method Source Code

//package com.java2s;

public class Main {

    public static int toInteger(byte[] b, int pos) {
        int ret = 0;
        for (int i = 0; i < 4; i++) {
            ret |= (b[i + pos] & 0xFF) << (8 * i);
        }//from  w ww.  j  av a2s  . co  m
        return ret;
    }

    public static int toInteger(byte[] b, int pos, int width) {
        int retVal = Integer.MAX_VALUE;
        switch (width) {
        case 1:
            retVal = b[pos];
            if (retVal < 0) {
                retVal &= 0x000000FF;
            }
            break;
        case 2:
            retVal = toIntFromTwoBytes(b, pos);
            break;
        case 4:
            retVal = toInteger(b, pos);
            break;
        default:
            break;
        }

        return retVal;
    }

    public static int toIntFromTwoBytes(byte[] b, int pos) {
        int ret = 0;
        ret |= (b[pos] & 0xFF);
        ret |= (b[pos + 1] & 0xFF) << 8;

        return (int) ret;
    }
}

Related

  1. getUIntByByteArray(byte[] data)
  2. getUIntByByteArray(byte[] data, boolean isLH)
  3. toInt(byte[] src)
  4. toInt(byte[] src, int srcPos)
  5. toIntFromTwoBytes(byte[] b, int pos)
  6. toInteger(byte[] b, int pos, int width)
  7. toInts(byte... bytes)
  8. byteArrayToInt(byte[] b)
  9. bytes2int(byte[] data)