class Bytes {

    public static int toInt(byte[] pb, boolean bigEndian) {

        switch (pb.length) {
            case 1:
                return pb[0] & 0xff;
            case 2:
                if (bigEndian)
                    return (pb[0] << 8 & 0xff00) | (pb[1] & 0xff);
                else
                    return (pb[1] << 8 & 0xff00) | (pb[0] & 0xff);
            case 3:
                if (bigEndian)
                    return (pb[0] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[2] & 0xff);
                else
                    return (pb[2] << 16 & 0xff0000) | (pb[1] << 8 & 0xff00) | (pb[0] & 0xff);
            case 4:
                if (bigEndian) {
                    return (pb[0] << 24 & 0xff000000) |
                            (pb[1] << 16 & 0xff0000) |
                            (pb[2] << 8 & 0xff00) |
                            (pb[3] & 0xff);
                } else {
                    return (pb[3] << 24 & 0xff000000) |
                            (pb[2] << 16 & 0xff0000) |
                            (pb[1] << 8 & 0xff00) |
                            (pb[0] & 0xff);
                }
            default:
                throw new IllegalArgumentException("1,2,3 or 4 byte arrays allowed. size:" + pb.length);
        }
    }

}