Java Integer to Byte Array convertIntToByteArray(int[] rgb)

Here you can find the source of convertIntToByteArray(int[] rgb)

Description

Converts the given int[] array into a byte[] array

License

Open Source License

Parameter

Parameter Description
rgb the original integer array

Return

the corresponding byte array in which integers are in the same way encoded as in DataOutputStream.writeInt()

Declaration

public static byte[] convertIntToByteArray(int[] rgb) 

Method Source Code

//package com.java2s;
/*//w  ww  .j  a v  a2s.  co m
 * Created on Jul 23, 2007 at 10:53:12 AM.
 * 
 * Copyright (c) 2010 Robert Virkus / Enough Software
 *
 * This file is part of J2ME Polish.
 *
 * J2ME Polish 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.
 * 
 * J2ME Polish 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 J2ME Polish; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Commercial licenses are also available, please
 * refer to the accompanying LICENSE.txt or visit
 * http://www.j2mepolish.org for details.
 */

public class Main {
    /**
     * Converts the given int[] array into a byte[] array
     * @param rgb the original integer array
     * @return the corresponding byte array in which integers are in the same way encoded as in DataOutputStream.writeInt()
     */
    public static byte[] convertIntToByteArray(int[] rgb) {
        byte[] data = new byte[rgb.length * 4];
        int j = 0;
        for (int i = 0; i < rgb.length; i++) {
            int v = rgb[i];
            data[j + 0] = (byte) ((v >>> 24) & 0xFF);
            data[j + 1] = (byte) ((v >>> 16) & 0xFF);
            data[j + 2] = (byte) ((v >>> 8) & 0xFF);
            data[j + 3] = (byte) ((v >>> 0) & 0xFF);
            j += 4;
        }
        return data;
    }
}

Related

  1. convertIntegersToBytes(int[] integers)
  2. convertIntegerToByteArray(int value, int bytes, boolean revers)
  3. convertIntToByteArray(int value)
  4. convertIntToByteArray(int value, int numberOfBytes)
  5. convertIntToBytes(int i)
  6. fromIntToByte(int integer)
  7. fromIntToByteArray(int value)
  8. int2Arr(int var, byte[] arrayBytes, int startIndex)