Java Integer to Byte Array intToBytes(int i, byte[] data, int[] offset)

Here you can find the source of intToBytes(int i, byte[] data, int[] offset)

Description

Write the bytes representing i into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

License

Open Source License

Parameter

Parameter Description
i the <code>int</code> to encode
data The byte array to store into, or <code>null</code>.
offset A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

Declaration

public static final void intToBytes(int i, byte[] data, int[] offset) 

Method Source Code

//package com.java2s;
/*/*from  w  ww .j  ava  2s. c  om*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library 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 Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    public static final int SIZE_INT = 4;

    /**
     * Write the bytes representing <code>i</code> into the byte array
     * <code>data</code>, starting at index <code>offset [0]</code>, and
     * increment <code>offset [0]</code> by the number of bytes written; if
     * <code>data == null</code>, increment <code>offset [0]</code> by the
     * number of bytes that would have been written otherwise.
     *
     * @param i the <code>int</code> to encode
     * @param data The byte array to store into, or <code>null</code>.
     * @param offset A single element array whose first element is the index in
     *         data to begin writing at on function entry, and which on
     *         function exit has been incremented by the number of bytes
     *         written.
     */
    public static final void intToBytes(int i, byte[] data, int[] offset) {
        /**
         * TODO: We use network-order within OceanStore, but temporarily
         * supporting intel-order to work with some JNI code until JNI code is
         * set to interoperate with network-order.
         */
        if (data != null) {
            for (int j = (offset[0] + SIZE_INT) - 1; j >= offset[0]; --j) {
                data[j] = (byte) i;
                i >>= 8;
            }
        }

        offset[0] += SIZE_INT;
    }
}

Related

  1. intToBytes(int i)
  2. intToBytes(int i)
  3. intToBytes(int i)
  4. IntToBytes(int i)
  5. intToBytes(int i)
  6. intToBytes(int i_)
  7. intToBytes(int intValue)
  8. intToBytes(int ipInt)
  9. intToBytes(int n)