Java Convert via ByteBuffer intToByteArray(int l)

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

Description

Converts an int to a byte array of length 4 containing the integer encoded in two's complement.

License

Open Source License

Parameter

Parameter Description
l the integer to convert

Return

the resulting byte array

Declaration

private static byte[] intToByteArray(int l) 

Method Source Code

//package com.java2s;
/*//from w  ww  .j  a v  a  2s . c  o m
 *  This file is part of "Tweety", a collection of Java libraries for
 *  logical aspects of artificial intelligence and knowledge representation.
 *
 *  Tweety is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.IntBuffer;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Converts an {@code int} to a {@code byte} array of length 4 containing the integer encoded in two's complement.
     * @param l the integer to convert
     * @return the resulting {@code byte} array
     */
    private static byte[] intToByteArray(int l) {
        byte[] bArray = new byte[4];
        ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
        IntBuffer iBuffer = bBuffer.asIntBuffer();
        iBuffer.put(0, l);
        return bArray;
    }
}

Related

  1. intArrayToIntBuffer(int[] data)
  2. intsToBytes(int[] n)
  3. IntToBArray(int src)
  4. intToByte(int[] intArray)
  5. intToByteArray(int integer)
  6. intToByteArray(int number)
  7. intToByteArray(int someInt, int byteSize)
  8. intToBytes(final int i)
  9. intToBytes(final int integer)