Java Integer to Byte Array int2bytes(int integer)

Here you can find the source of int2bytes(int integer)

Description

Split an int into 4 bytes.

License

Open Source License

Parameter

Parameter Description
integer an int, eg -2^31, or 16.

Return

the 4 bytes of its two's complement, eg [0x80, 0x00, 0x00, 0x00] or [0x00, 0x00, 0x00, 0x10].

Declaration

public static byte[] int2bytes(int integer) 

Method Source Code

//package com.java2s;
/*/*from  w w  w . j a  v a 2s. c  o m*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */

public class Main {
    /**
     * Split an int into 4 bytes.
     * 
     * @param integer an int, eg -2^31, or 16.
     * @return the 4 bytes of its two's complement, eg [0x80, 0x00, 0x00, 0x00] or [0x00, 0x00,
     *         0x00, 0x10].
     */
    public static byte[] int2bytes(int integer) {
        final byte[] byteStr = new byte[4];
        byteStr[0] = (byte) (integer >>> 24);
        byteStr[1] = (byte) ((integer >>> 16) & 0xff);
        byteStr[2] = (byte) ((integer >>> 8) & 0xff);
        byteStr[3] = (byte) (integer & 0xff);
        return byteStr;
    }
}

Related

  1. fromIntToByteArray(int value)
  2. int2Arr(int var, byte[] arrayBytes, int startIndex)
  3. int2ByteLE(byte[] bytes, int value, int offset)
  4. int2bytes(int i)
  5. int2Bytes(int i, byte[] bytes, int offset)
  6. int2bytes(int intValue)
  7. int2Bytes(int l, byte[] target, int offset)
  8. int2Bytes(int n, byte[] dst, int start)
  9. int2Bytes(int num)