Java Integer to Byte Array intToBytes(int value)

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

Description

Converts an int into an array of 4 bytes.

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration

public static byte[] intToBytes(int value) 

Method Source Code

//package com.java2s;
// modify it under  the terms of  the GNU General Public License

public class Main {
    /**/*from   w w  w .ja  v a 2  s .  c om*/
     * Converts an <code>int</code> into an array of
     * 4 bytes.
     * TODO: add a switch for endianess
     * @param value
     * @return
     */
    public static byte[] intToBytes(int value) {
        byte[] out = new byte[4];
        out[0] = (byte) (value >> 0);
        out[1] = (byte) (value >> 8);
        out[2] = (byte) (value >> 16);
        out[3] = (byte) (value >> 24);
        return out;
    }
}

Related

  1. intToBytes(int val)
  2. intToBytes(int val, byte[] bytes, int start)
  3. intToBytes(int val, int byteCount)
  4. intToBytes(int value)
  5. intToBytes(int value)
  6. intToBytes(int value, byte[] array, int offset)
  7. intToBytes(int value, byte[] buffer, int offset)
  8. intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian)
  9. intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)