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

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

Description

to Big Endian Byte Array

Declaration

public static void toBigEndianByteArray(int val, byte[] b, int pos) 

Method Source Code

//package com.java2s;

public class Main {

    public static void toBigEndianByteArray(int val, byte[] b, int pos) {
        assert (pos + 4 <= b.length);
        for (int i = 3; i >= 0; i--) {
            b[pos + i] = (byte) (val & 0x000000FF);
            val = val >> 8;
        }/*from w  ww .j  a  v a 2 s. c o m*/
    }

    public static void toBigEndianByteArray(short val, byte[] b, int pos) {
        assert (pos + 2 <= b.length);
        b[pos + 1] = (byte) (val & 0x00FF);
        b[pos] = (byte) ((val & 0xFF00) >> 8);
    }

    public static void toBigEndianByteArray(long val, byte[] b, int pos) {
        assert (pos + 8 <= b.length);
        for (int i = 7; i >= 0; i--) {
            b[pos + i] = (byte) (val & 0x00000000000000FFl);
            val = val >> 8;
        }
    }
}

Related

  1. intAsBigEnd(int val)
  2. longAsBigEnd(byte[] a, int i, long v)
  3. longAsBigEnd(long val)
  4. shortAsBigEnd(byte[] a, int i, short v)
  5. shortAsBigEnd(short val)
  6. toBigEndianByteArray(long val, byte[] b, int pos)
  7. toBigEndianByteArray(short val, byte[] b, int pos)
  8. toBigEndianBytes(int x)
  9. toBigEndianBytes(long x)