Java Integer to Byte Array intToByteArray(int value)

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

Description

Converts the given integer value into a byte array.

License

Mozilla Public License

Parameter

Parameter Description
value Integer value to convert to byte array.

Return

Byte array of the given integer (4 bytes length).

Declaration

public static byte[] intToByteArray(int value) 

Method Source Code

//package com.java2s;
/**/*  www .  j a  va  2s  .  co m*/
 * Copyright (c) 2014-2016 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */

public class Main {
    /**
     * Converts the given integer value into a byte array.
     * 
     * @param value Integer value to convert to byte array.
     * 
     * @return Byte array of the given integer (4 bytes length).
     * 
     * @see #byteArrayToInt(byte[])
     */
    public static byte[] intToByteArray(int value) {
        return new byte[] { (byte) ((value >>> 24) & 0xFF), (byte) ((value >>> 16) & 0xFF),
                (byte) ((value >>> 8) & 0xFF), (byte) (value & 0xFF) };
    }
}

Related

  1. intToByteArray(int value)
  2. intToByteArray(int value)
  3. intToByteArray(int value)
  4. intToByteArray(int value)
  5. intToByteArray(int value)
  6. intToByteArray(int value, byte[] buffer, int offset)
  7. intToByteArray(int value, byte[] data, int offset)
  8. intToByteArray(int value, byte[] dest)
  9. intToByteArray(int value, int nrOfBytes)