Android Big Endian Convert toBigEndianBytes(int x)

Here you can find the source of toBigEndianBytes(int x)

Description

to Big Endian Bytes

License

Open Source License

Declaration

public static byte[] toBigEndianBytes(int x) 

Method Source Code

//package com.java2s;
/*/* w  w  w . j  a  v  a  2 s . c  om*/
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group
 */

public class Main {
    public static byte[] toBigEndianBytes(long x) {
        byte[] arr = new byte[8];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (byte) (x >>> (64 - (i + 1) * 8));
        }
        return arr;
    }

    public static byte[] toBigEndianBytes(int x) {
        return new byte[] { (byte) (x >>> 24), (byte) (x >>> 16),
                (byte) (x >>> 8), (byte) x };
    }

    public static byte[] toBigEndianBytes(short x) {
        return new byte[] { (byte) (x >>> 8), (byte) x };
    }
}

Related

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