Java Byte Array Create toByteArray(int value)

Here you can find the source of toByteArray(int value)

Description

Converts an int into a 4-byte array in Big Endian order (most-significant byte in slot 0)

License

Open Source License

Parameter

Parameter Description
value Integer to convert

Return

4-byte array in Big Endian order (most-significant byte in slot 0)

Declaration

public static byte[] toByteArray(int value) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .j  a v a2  s  .  co  m*/
 * File:                HashFunctionUtil.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright Jan 26, 2011, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government.
 * Export of this program may require a license from the United States
 * Government. See CopyrightHistory.txt for complete details.
 *
 */

public class Main {
    /**
     * Converts an int into a 4-byte array in Big Endian order
     * (most-significant byte in slot 0)
     * @param value
     * Integer to convert
     * @return
     * 4-byte array in Big Endian order (most-significant byte in slot 0)
     */
    public static byte[] toByteArray(int value) {
        byte[] output = new byte[4];
        toByteArray(value, output);
        return output;
    }

    /**
     * Converts an int into a 4-byte array in Big Endian order
     * (most-significant byte in slot 0)
     * @param value
     * Integer to convert
     * @param output
     * 4-byte array to store the int
     */
    public static void toByteArray(int value, byte[] output) {
        if (output.length != 4) {
            throw new IllegalArgumentException("Expected output array of length 4, got length = " + output.length);
        }
        output[0] = (byte) (value >>> 24);
        output[1] = (byte) (value >>> 16);
        output[2] = (byte) (value >>> 8);
        output[3] = (byte) (value);
    }

    /**
     * Converts a long into an 8-byte array in Big Endian order
     * (most-significant byte in slot 0)
     * @param value
     * long to convert
     * @return
     * 8-byte array to store the long
     */
    public static byte[] toByteArray(long value) {
        byte[] output = new byte[8];
        toByteArray(value, output);
        return output;
    }

    /**
     * Converts a long into an 8-byte array in Big Endian order
     * (most-significant byte in slot 0)
     * @param value
     * long to convert
     * @param output
     * 8-byte array to store the long
     */
    public static void toByteArray(long value, byte[] output) {
        if (output.length != 8) {
            throw new IllegalArgumentException("Expected output array of length 8, got length = " + output.length);
        }
        output[0] = (byte) (value >>> 56);
        output[1] = (byte) (value >>> 48);
        output[2] = (byte) (value >>> 40);
        output[3] = (byte) (value >>> 32);
        output[4] = (byte) (value >>> 24);
        output[5] = (byte) (value >>> 16);
        output[6] = (byte) (value >>> 8);
        output[7] = (byte) (value);
    }
}

Related

  1. toByteArray(int intr)
  2. toByteArray(int num)
  3. toByteArray(int value)
  4. toByteArray(int value)
  5. toByteArray(int value)
  6. toByteArray(int value)
  7. toByteArray(int value, int numBytes, byte[] dest, int off)
  8. toByteArray(int[] array)
  9. toByteArray(int[] data)