Java Integer Array to Byte Array intArrayToByteArray(int[] ints)

Here you can find the source of intArrayToByteArray(int[] ints)

Description

Converts image int[] array (32-bit per pixel) to byte[] xRGB array (4 bytes per pixel) int<->byte[4] conversion is platform specific !

License

Open Source License

Parameter

Parameter Description
ints integer array to convert

Return

same array but converted to byte[] (in big-endian format)

Declaration

public static byte[] intArrayToByteArray(int[] ints) 

Method Source Code

//package com.java2s;
/*/*from  w ww  . j av a  2s  .  c om*/
 * Copyright  1990-2009 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * 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 version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */

public class Main {
    /**
     * Converts image int[] array (32-bit per pixel) 
     * to byte[] xRGB array (4 bytes per pixel) 
     * int<->byte[4] conversion is platform specific !  
     *
     * @param ints integer array to convert
     * @return same array but converted to byte[] (in big-endian format)
     */
    public static byte[] intArrayToByteArray(int[] ints) {
        if (ints == null)
            return null;

        byte[] bytes = new byte[ints.length * 4];

        int intcount, bytecount;
        for (intcount = 0, bytecount = 0; intcount < ints.length;) {
            bytes[bytecount++] = (byte) ((ints[intcount] & 0xFF000000) >> 24);
            bytes[bytecount++] = (byte) ((ints[intcount] & 0x00FF0000) >> 16);
            bytes[bytecount++] = (byte) ((ints[intcount] & 0x0000FF00) >> 8);
            bytes[bytecount++] = (byte) ((ints[intcount] & 0x000000FF));
            intcount++;
        }
        return bytes;
    }
}

Related

  1. convertIntArrayToByteArray(int[] data)
  2. convertIntArrayToByteArray(int[] myIntArray, int bytesPerInt)
  3. intArray2Bytes(int[] array)
  4. intArrayToByteArray(int[] ai)
  5. intArrayToByteArray(int[] data)
  6. intArrayToBytes(int[] d)