Java Integer to Byte Array int2Arr(int var, byte[] arrayBytes, int startIndex)

Here you can find the source of int2Arr(int var, byte[] arrayBytes, int startIndex)

Description

Write the bytes of "var" into new byte array.

License

Open Source License

Parameter

Parameter Description
var the int to encode
arrayBytes The byte array to store into.
startIndex index data to begin write.

Declaration

public static void int2Arr(int var, byte[] arrayBytes, int startIndex) 

Method Source Code

//package com.java2s;
/* DielmoOpenLiDAR//from   w w  w  .java 2s  .  c om
 *
 * Copyright (C) 2008 DIELMO 3D S.L. (DIELMO) and Infrastructures  
 * and Transports Department of the Valencian Government (CIT)
 * 
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * For more information, contact:
 *
 * DIELMO 3D S.L.
 * Plaza Vicente Andr?s Estell?s 1 Bajo E
 * 46950 Xirivella, Valencia
 * SPAIN
 *   
 * +34 963137212
 * dielmo@dielmo.com
 * www.dielmo.com
 * 
 * or
 * 
 * Generalitat Valenciana
 * Conselleria d'Infraestructures i Transport
 * Av. Blasco Ib??ez, 50
 * 46010 VALENCIA
 * SPAIN
 *
 * +34 963862235
 * gvsig@gva.es
 * www.gvsig.gva.es
 */

public class Main {
    /**
     * Write the bytes of "var" into new byte array.
     *
     * @param var the int to encode
     * @param arrayBytes The byte array to store into.
     * @param startIndex index data to begin write.
     */
    public static void int2Arr(int var, byte[] arrayBytes, int startIndex) {

        int length = 4;

        if (arrayBytes != null && startIndex + length <= arrayBytes.length) {
            for (int j = startIndex; j < startIndex + length; j++) {
                arrayBytes[j] = (byte) var; // se copian los 8 primeros bits de la variable
                var >>= 8;
            }
        }
    }
}

Related

  1. convertIntToByteArray(int value, int numberOfBytes)
  2. convertIntToByteArray(int[] rgb)
  3. convertIntToBytes(int i)
  4. fromIntToByte(int integer)
  5. fromIntToByteArray(int value)
  6. int2ByteLE(byte[] bytes, int value, int offset)
  7. int2bytes(int i)
  8. int2Bytes(int i, byte[] bytes, int offset)
  9. int2bytes(int integer)