Here you can find the source of int2bytes(int integer)
Parameter | Description |
---|---|
integer | an int, eg -2^31, or 16. |
public static byte[] int2bytes(int integer)
//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; } }