Java Integer to Byte Array intToBytes(int num, byte[] arr, int pos)

Here you can find the source of intToBytes(int num, byte[] arr, int pos)

Description

Writes an integer to 4 bytes in big-endian notation to the byte array.

License

Open Source License

Parameter

Parameter Description
num the integer to write.
arr the byte array to place the int in.
pos the index of the first byte in the 4 byte sequence.

Exception

Parameter Description
IllegalArgumentException if the position is out of the range of the byte array.

Declaration

public static void intToBytes(int num, byte[] arr, int pos)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*from w w w  . j a  v  a 2s . co  m*/
 * Process Sketcher is a simple flowchart making software.
 * Copyright (C) 2015  Jonathon Prehn, Kevin Prehn
 * 
 * This file is a part of Process Sketcher.
 * 
 * Process Sketcher 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.
 * 
 * Process Sketcher 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 Process Sketcher.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Writes an integer to 4 bytes in big-endian notation to the byte array.
     *
     * @param num the integer to write.
     * @param arr the byte array to place the int in.
     * @param pos the index of the first byte in the 4 byte sequence.
     * @throws IllegalArgumentException if the position is out of the 
     * range of the byte array.
     */
    public static void intToBytes(int num, byte[] arr, int pos)
            throws IllegalArgumentException {
        checkBounds(arr, pos);
        arr[pos] = (byte) ((num & 0xFF000000) >> 24);
        arr[pos + 1] = (byte) ((num & 0x00FF0000) >> 16);
        arr[pos + 2] = (byte) ((num & 0x0000FF00) >> 8);
        arr[pos + 3] = (byte) ((num & 0x000000FF));
    }

    /**
     * Checks the position to the bounds of the byte array.
     * @param arr the byte array
     * @param pos the position in the byte array
     * @throws IllegalArgumentException thrown if the position is out of the 
     * byte array's bounds.
     */
    private static void checkBounds(byte[] arr, int pos)
            throws IllegalArgumentException {
        if (pos < 0 || pos >= arr.length) {
            throw new IllegalArgumentException(
                    "Position is out of bounds of "
                            + "the byte array: position is " + pos
                            + " while the byte"
                            + " array only accepts 0 to "
                            + (arr.length - 1));
        }
    }
}

Related

  1. intToBytes(int i_)
  2. intToBytes(int intValue)
  3. intToBytes(int ipInt)
  4. intToBytes(int n)
  5. intToBytes(int n)
  6. intToBytes(int num, byte[] bytes, int startIndex)
  7. intToBytes(int number, byte[] destination, int destinationIndex)
  8. intToBytes(int v)
  9. intToBytes(int v)