Here you can find the source of toByteArray(final int i)
Parameter | Description |
---|---|
i | the int to be converted. |
public static byte[] toByteArray(final int i)
//package com.java2s; /*//from w w w . j a v a2s . c o m (c) copyright This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ public class Main { /** * Constant to represent the big endian format (the format used internally * in Java). **/ public static boolean BIG_ENDIAN = true; /** * Converts an int to a byte[4] array. The * first byte (0) in the returned array contain the most significant * eigth bits of the supplied int, ie big-endian. * * @param i the int to be converted. * @return a byte[4] array. **/ public static byte[] toByteArray(final int i) { return toByteArray(i, BIG_ENDIAN); } /** * Converts an int to a byte[4] array. If * format is true the first byte (0) in the returned array contain the * least significant eigth bits of the supplied int, ie little-endian. if * format is false the conversion is big-endian. * * Note: the BIG_ENDIAN and LITTLE_ENDIAN should be used in the * format parameter. * * @param i the int to be converted. * @param format the wanted format of the retured byte array. * @return a byte[4] array. **/ public static byte[] toByteArray(final int i, final boolean format) { final byte[] ba = new byte[4]; if (format) { // little-endian ba[0] = (byte) (i & 0x000000ff); ba[1] = (byte) ((i & 0x0000ff00) >> 8); ba[2] = (byte) ((i & 0x00ff0000) >> (8 * 2)); ba[3] = (byte) ((i & 0xff000000) >> (8 * 3)); } else { // big-endian ba[0] = (byte) ((i & 0xff000000) >> (8 * 3)); ba[1] = (byte) ((i & 0x00ff0000) >> (8 * 2)); ba[2] = (byte) ((i & 0x0000ff00) >> 8); ba[3] = (byte) (i & 0x000000ff); } return ba; } /** * Converts a float to a byte[4] array by placing the bits * in the float into bytes and placing them after each other in the same * order as Java stores floats, ie big-endian. * * @param f the float to be converted. * @return the resulting value as an byte[4] array. **/ public static byte[] toByteArray(final float f) { final int i = Float.floatToRawIntBits(f); return toByteArray(i); } /** * Converts a float to a byte[4] array by placing the bits * in the float into bytes and placing them after each other in the same * order as Java stores floats, ie big-endian. * * Note: the constants BIG_ENDIAN and LITTLE_ENDIAN (format) should be * used in the format parameter. * * @param f the float to be converted. * @param format the wanted format of the returned byte array. * @return the resulting value as an byte[4] array. **/ public static byte[] toByteArray(final float f, final boolean format) { final int i = Float.floatToRawIntBits(f); return toByteArray(i, format); } }