Java Integer to Byte Array intToBytes(int number, byte[] destination, int destinationIndex)

Here you can find the source of intToBytes(int number, byte[] destination, int destinationIndex)

Description

Convert an Integer to four bytes

License

Open Source License

Parameter

Parameter Description
number the number to convert to a 4 byte integer
destination the destination byte array to insert the new 4-bytes into
destinationIndex the index location where the number should be copies

Declaration

public static void intToBytes(int number, byte[] destination, int destinationIndex) 

Method Source Code

//package com.java2s;
/*/*from   ww w  . java 2s  .  co  m*/
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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/>.
 */

public class Main {
    /**
     * Convert an Integer to four bytes
     *
     * @param number           the number to convert to a 4 byte integer
     * @param destination      the destination byte array to insert the new
     *                         4-bytes into
     * @param destinationIndex the index location where the number should be
     *                         copies
     */
    public static void intToBytes(int number, byte[] destination, int destinationIndex) {
        if (destination == null) {
            return;
        }
        int idx = destinationIndex;
        final int MASK = 0x000000ff;
        destination[idx++] = (byte) ((number >>> 24) & MASK);
        destination[idx++] = (byte) ((number >>> 16) & MASK);
        destination[idx++] = (byte) ((number >>> 8) & MASK);
        destination[idx] = (byte) (number & MASK);
    }
}

Related

  1. intToBytes(int ipInt)
  2. intToBytes(int n)
  3. intToBytes(int n)
  4. intToBytes(int num, byte[] arr, int pos)
  5. intToBytes(int num, byte[] bytes, int startIndex)
  6. intToBytes(int v)
  7. intToBytes(int v)
  8. intToBytes(int v, byte[] bytes)
  9. intToBytes(int v, final byte[] arr)