Java Integer to Byte Array intToBytes(final int integer, final int bLength)

Here you can find the source of intToBytes(final int integer, final int bLength)

Description

convert integer to byte array

License

Open Source License

Parameter

Parameter Description
integer the input integer to convert
bLength the length of result bytes array

Return

byte array representing for the integer (Networking order)

Declaration

public static byte[] intToBytes(final int integer, final int bLength) 

Method Source Code

//package com.java2s;
/**//from   w  w w. j  av a2  s .  c  o  m
 *    Created on Oct 21, 2010
 *    This file is part of JObexFTP 2.0, and it contains parts of OBEX4J.
 *
 *    JObexFTP is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    JObexFTP 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 Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with JObexFTP.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * convert integer to byte array
     *
     * @param integer the input integer to convert
     * @param bLength the length of result bytes array
     * @return byte array representing for the integer (Networking order)
     */
    public static byte[] intToBytes(final int integer, final int bLength) {
        if (integer > Integer.MAX_VALUE || integer < Integer.MIN_VALUE) {
            return null;
        }
        byte[] bytes = new byte[bLength];
        for (int i = 0; i < bLength; i++) {
            bytes[i] = new Integer(integer >> (bLength - 1 - i) * 8).byteValue();
        }
        return bytes;
    }
}

Related

  1. intToByteMSB(int in)
  2. intToBytes(byte[] arr, int offset, int num)
  3. intToBytes(byte[] bytes, int index, int value)
  4. intToBytes(final int aInt)
  5. intToBytes(final int i)
  6. intToBytes(final int number)
  7. intToBytes(final int value)
  8. intTobytes(final int value)
  9. intToBytes(int a, byte[] b, int bo)