Java Byte Array Create toByteArray(final int i)

Here you can find the source of toByteArray(final int i)

Description

Returns the bytes (big-endian) of an integer.

License

Open Source License

Parameter

Parameter Description
i The integer.

Return

A 4-byte array of the bytes of the integer.

Declaration

public static byte[] toByteArray(final int i) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// ww w  .j  a va 2  s  .  com
     * Returns the bytes (big-endian) of an integer.
     *
     * @param i The integer.
     *
     * @return A 4-byte array of the bytes of the integer.
     */
    public static byte[] toByteArray(final int i) {
        final byte[] b = new byte[4];

        b[0] = (byte) ((i & 0xff000000) >>> 24);
        b[1] = (byte) ((i & 0x00ff0000) >>> 16);
        b[2] = (byte) ((i & 0x0000ff00) >>> 8);
        b[3] = (byte) ((i & 0x000000ff));

        return b;
    }

    /**
     * Converts the specified long to an array of bytes.
     * 
     * @param l The long to convert.
     * @return The array of bytes with the most significant byte first.
     */
    public static byte[] toByteArray(final long l) {
        final byte[] bytes = new byte[8];
        bytes[0] = (byte) ((l >>> 56) & 0xff);
        bytes[1] = (byte) ((l >>> 48) & 0xff);
        bytes[2] = (byte) ((l >>> 40) & 0xff);
        bytes[3] = (byte) ((l >>> 32) & 0xff);
        bytes[4] = (byte) ((l >>> 24) & 0xff);
        bytes[5] = (byte) ((l >>> 16) & 0xff);
        bytes[6] = (byte) ((l >>> 8) & 0xff);
        bytes[7] = (byte) ((l >>> 0) & 0xff);
        return bytes;
    }
}

Related

  1. toByteArray(final byte[] bytes)
  2. toByteArray(final byte[][] array)
  3. toByteArray(final char[] array)
  4. toByteArray(final char[] array)
  5. toByteArray(final int i)
  6. toByteArray(final int i, final byte[] output, final int offset)
  7. toByteArray(final int value)
  8. toByteArray(final String bitString)
  9. toByteArray(final String hexString)