Android Big Endian Convert toBigEndianInteger(byte[] b, int pos)

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

Description

to Big Endian Integer

Declaration

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

Method Source Code

//package com.java2s;

public class Main {

    public static int toBigEndianInteger(byte[] b, int pos) {
        int ret = 0;
        for (int i = 0; i < 4; i++) {
            ret |= (b[i + pos] & 0xFF) << (8 * (3 - i));
        }//from   w  w  w  .  j av  a2 s  .c  o m
        return ret;
    }

    public static int toBigEndianInteger(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 = toBigEndianIntFromTwoBytes(b, pos);
            break;
        case 4:
            retVal = toBigEndianInteger(b, pos);
            break;
        default:
            break;
        }

        return retVal;
    }

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

        return (int) ret;
    }
}

Related

  1. toBigEndianByteArray(short val, byte[] b, int pos)
  2. toBigEndianBytes(int x)
  3. toBigEndianBytes(long x)
  4. toBigEndianBytes(short x)
  5. toBigEndianIntFromTwoBytes(byte[] b, int pos)
  6. toBigEndianInteger(byte[] b, int pos, int width)
  7. toBigEndianLong(byte[] b, int pos, int width)
  8. toBigEndianShort(byte[] b, int pos)
  9. toggleIntEndian(byte[] b)