Android Utililty Methods Big Endian Convert

List of utility methods to do Big Endian Convert

Description

The list of methods to do Big Endian Convert are organized into topic(s).

Method

voidtoBigEndianByteArray(short val, byte[] b, int pos)
to Big Endian Byte Array
assert (pos + 2 <= b.length);
b[pos + 1] = (byte) (val & 0x00FF);
b[pos] = (byte) ((val & 0xFF00) >> 8);
byte[]toBigEndianBytes(int x)
to Big Endian Bytes
return new byte[] { (byte) (x >>> 24), (byte) (x >>> 16),
        (byte) (x >>> 8), (byte) x };
byte[]toBigEndianBytes(long x)
to Big Endian Bytes
byte[] arr = new byte[8];
for (int i = 0; i < arr.length; i++) {
    arr[i] = (byte) (x >>> (64 - (i + 1) * 8));
return arr;
byte[]toBigEndianBytes(short x)
to Big Endian Bytes
return new byte[] { (byte) (x >>> 8), (byte) x };
inttoBigEndianIntFromTwoBytes(byte[] b, int pos)
to Big Endian Int From Two Bytes
int ret = 0;
ret |= (b[pos + 1] & 0xFF);
ret |= (b[pos] & 0xFF) << 8;
return (int) ret;
inttoBigEndianInteger(byte[] b, int pos)
to Big Endian Integer
int ret = 0;
for (int i = 0; i < 4; i++) {
    ret |= (b[i + pos] & 0xFF) << (8 * (3 - i));
return ret;
inttoBigEndianInteger(byte[] b, int pos, int width)
to Big Endian Integer
int retVal = Integer.MAX_VALUE;
switch (width) {
case 1:
    retVal = b[pos];
    if (retVal < 0) {
        retVal &= 0x000000FF;
    break;
...
longtoBigEndianLong(byte[] b, int pos, int width)
to Big Endian Long
long ret = 0;
for (int i = 0; i < width; i++) {
    ret |= (b[i + pos] & 0xFFl) << (8 * (width - i - 1));
return ret;
shorttoBigEndianShort(byte[] b, int pos)
to Big Endian Short
short ret = 0;
ret |= (b[pos] & 0xFF) << 8;
ret |= (b[pos + 1] & 0xFF);
return ret;
voidtoggleIntEndian(byte[] b)
toggle Int Endian
if (b != null)
    toggleIntEndian(b, 0, b.length);