Android Big Endian Convert toBigEndianBytes(long x)

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

Description

to Big Endian Bytes

License

Open Source License

Declaration

public static byte[] toBigEndianBytes(long x) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  . j  av a2  s . com*/
 * 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(short val)
  2. toBigEndianByteArray(int val, byte[] b, int pos)
  3. toBigEndianByteArray(long val, byte[] b, int pos)
  4. toBigEndianByteArray(short val, byte[] b, int pos)
  5. toBigEndianBytes(int x)
  6. toBigEndianBytes(short x)
  7. toBigEndianIntFromTwoBytes(byte[] b, int pos)
  8. toBigEndianInteger(byte[] b, int pos)
  9. toBigEndianInteger(byte[] b, int pos, int width)