Java Byte Array Create toByteArray(final int i)

Here you can find the source of toByteArray(final int i)

Description

Converts an int to a byte[4] array.

License

Open Source License

Parameter

Parameter Description
i the int to be converted.

Return

a byte[4] array.

Declaration

public static byte[] toByteArray(final int i) 

Method Source Code

//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);
    }
}

Related

  1. toByteArray(double[][] array)
  2. toByteArray(final byte[] bytes)
  3. toByteArray(final byte[][] array)
  4. toByteArray(final char[] array)
  5. toByteArray(final char[] array)
  6. toByteArray(final int i)
  7. toByteArray(final int i, final byte[] output, final int offset)
  8. toByteArray(final int value)
  9. toByteArray(final String bitString)