Mega Code Archive

 
Categories / Java / File Input Output
 

Converts an int integer array to a byte array

/*  *  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>  */ import java.io.File; import java.io.IOException; /**  * Various utility functions.  */ public class Util {     /**      * 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;         }     }   }