Feb 26, 2009

Converting 32 bit integer into a byte array

/**
* Convert an int into its little-endian byte string representation.
*
* @param array the array in which to store the byte string.
* @param offset the offset in the array where the string will start.
* @param value the value to convert.
*/
public static void bytesFromInt(byte[] array, int offset, int value) {
array[offset+0] = (byte) ((value>>0) &0xFF);
array[offset+1] = (byte) ((value>>8) &0xFF);
array[offset+2] = (byte) ((value>>16)&0xFF);
array[offset+3] = (byte) ((value>>24)&0xFF);
}

No comments:

Down with the Dictatorship!

    "Let them hate me, so that they fear me" - Caligula 41AD