Android Int to Byte Array Convert getFourBytes(int i, byte[] target, int pos)

Here you can find the source of getFourBytes(int i, byte[] target, int pos)

Description

Converts an integer into four bytes, and places it in the array at the specified position

License

Open Source License

Parameter

Parameter Description
target the array which is to contain the converted data
pos the position in the array in which to place the data
i the integer to convert

Declaration

public static void getFourBytes(int i, byte[] target, int pos) 

Method Source Code

//package com.java2s;
/*********************************************************************
 *
 *      Copyright (C) 2002 Andrew Khan/*ww w. j a  va 2s .co  m*/
 *
 * 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 {
    /**
     * Gets a four byte array from an integer
     *
     * @param i the integer
     * @return a four byte array
     */
    public static byte[] getFourBytes(int i) {
        byte[] bytes = new byte[4];

        int i1 = i & 0xffff;
        int i2 = (i & 0xffff0000) >> 16;

        getTwoBytes(i1, bytes, 0);
        getTwoBytes(i2, bytes, 2);

        return bytes;
    }

    /**
     * Converts an integer into four bytes, and places it in the array at the
     * specified position
     *
     * @param target the array which is to contain the converted data
     * @param pos the position in the array in which to place the data
     * @param i the integer to convert
     */
    public static void getFourBytes(int i, byte[] target, int pos) {
        byte[] bytes = getFourBytes(i);
        target[pos] = bytes[0];
        target[pos + 1] = bytes[1];
        target[pos + 2] = bytes[2];
        target[pos + 3] = bytes[3];
    }

    /**
     * Gets a two byte array from an integer
     *
     * @param i the integer
     * @return the two bytes
     */
    public static byte[] getTwoBytes(int i) {
        byte[] bytes = new byte[2];

        bytes[0] = (byte) (i & 0xff);
        bytes[1] = (byte) ((i & 0xff00) >> 8);

        return bytes;
    }

    /**
     * Converts an integer into two bytes, and places it in the array at the
     * specified position
     *
     * @param target the array to place the byte data into
     * @param pos the position at which to place the data
     * @param i the integer value to convert
     */
    public static void getTwoBytes(int i, byte[] target, int pos) {
        target[pos] = (byte) (i & 0xff);
        target[pos + 1] = (byte) ((i & 0xff00) >> 8);
    }
}

Related

  1. toByteArray(int in)
  2. toByteArray(int in, int outSize)
  3. getTwoBytes(int i)
  4. getTwoBytes(int i, byte[] target, int pos)
  5. getFourBytes(int i)
  6. splitInt(int value)
  7. int2Byte(int value)
  8. int2ByteArray(int value)
  9. int2ByteArray1(int value)