Java Integer to Byte Array intToByteArray(final int x, final byte[] dst, final int offset)

Here you can find the source of intToByteArray(final int x, final byte[] dst, final int offset)

Description

Convert an integer to a byte array.

License

Open Source License

Parameter

Parameter Description
x The integer
dst The byte array (length = 4)
offset The offset of the encoded integer in the byte array

Declaration

public static final void intToByteArray(final int x, final byte[] dst, final int offset) 

Method Source Code

//package com.java2s;
/**//from  w  w w  . j a va  2s.c o  m
 * Copyright (c) 2014, Sindice Limited. All Rights Reserved.
 *
 * This file is part of the SIREn project.
 *
 * SIREn is a free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * SIREn 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert an integer to a byte array.
     *
     * @param x The integer
     * @param dst The byte array (length = 4)
     * @param offset The offset of the encoded integer in the byte array
     */
    public static final void intToByteArray(final int x, final byte[] dst, final int offset) {
        dst[offset + 3] = (byte) (x & 0x000000ff);
        dst[offset + 2] = (byte) ((x & 0x0000ff00) >>> 8);
        dst[offset + 1] = (byte) ((x & 0x00ff0000) >>> 16);
        dst[offset + 0] = (byte) ((x & 0xff000000) >>> 24);
    }
}

Related

  1. intToByteArray(final int integer)
  2. intToByteArray(final int v, final byte[] buf, final int offset)
  3. intToByteArray(final int val, final byte[] buf, final int offset)
  4. intToByteArray(final int value)
  5. intToByteArray(final int x)
  6. intToByteArray(int a)
  7. intToByteArray(int a)
  8. intToByteArray(int bytes)
  9. intToByteArray(int data)