Java Integer to Byte Array convertIntegersToBytes(int[] integers)

Here you can find the source of convertIntegersToBytes(int[] integers)

Description

This function converts an int integer array to a byte array.

License

Open Source License

Parameter

Parameter Description
integers an integer array

Return

a byte array containing the values of the int array. The byte array is 4x the length of the integer array.

Declaration

public static byte[] convertIntegersToBytes(int[] integers) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . j  av  a 2s .  c o m*/
 *  Tiled Map Editor, (c) 2004-2006
 *
 *  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.
 *
 *  Adam Turk <aturk@biggeruniverse.com>
 *  Bjorn Lindeijer <bjorn@lindeijer.nl>
 */

public class Main {
    /**
     * This function converts an <code>int</code> integer array to a
     * <code>byte</code> array. Each integer element is broken into 4 bytes and
     * stored in the byte array in litte endian byte order.
     *
     * @param integers an integer array
     * @return a byte array containing the values of the int array. The byte
     *         array is 4x the length of the integer array.
     */
    public static byte[] convertIntegersToBytes(int[] integers) {
        if (integers != null) {
            byte[] outputBytes = new byte[integers.length * 4];

            for (int i = 0, k = 0; i < integers.length; i++) {
                int integerTemp = integers[i];
                for (int j = 0; j < 4; j++, k++) {
                    outputBytes[k] = (byte) ((integerTemp >> (8 * j)) & 0xFF);
                }
            }
            return outputBytes;
        } else {
            return null;
        }
    }
}

Related

  1. convertIntegerToByteArray(int value, int bytes, boolean revers)
  2. convertIntToByteArray(int value)
  3. convertIntToByteArray(int value, int numberOfBytes)
  4. convertIntToByteArray(int[] rgb)