Here you can find the source of intToBytes(int number, byte[] destination, int destinationIndex)
Parameter | Description |
---|---|
number | the number to convert to a 4 byte integer |
destination | the destination byte array to insert the new 4-bytes into |
destinationIndex | the index location where the number should be copies |
public static void intToBytes(int number, byte[] destination, int destinationIndex)
//package com.java2s; /*/*from ww w . java 2s . co m*/ * Copyright (C) 2014 Jesse Caulfield * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Convert an Integer to four bytes * * @param number the number to convert to a 4 byte integer * @param destination the destination byte array to insert the new * 4-bytes into * @param destinationIndex the index location where the number should be * copies */ public static void intToBytes(int number, byte[] destination, int destinationIndex) { if (destination == null) { return; } int idx = destinationIndex; final int MASK = 0x000000ff; destination[idx++] = (byte) ((number >>> 24) & MASK); destination[idx++] = (byte) ((number >>> 16) & MASK); destination[idx++] = (byte) ((number >>> 8) & MASK); destination[idx] = (byte) (number & MASK); } }