Here you can find the source of toByteArray(int value)
Parameter | Description |
---|---|
value | The input integer. |
public static byte[] toByteArray(int value)
//package com.java2s; /*/*w ww . j av a2 s . c o m*/ This file is part of PH-Tree: A multi-dimensional indexing and storage structure. Copyright (C) 2011-2015 Eidgen?ssische Technische Hochschule Z?rich (ETH Zurich) Institute for Information Systems GlobIS Group Bogdan Vancea, Tilmann Zaeschke zaeschke@inf.ethz.ch or zoodb@gmx.de This program is 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. 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 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. The byte[] array is composed of the * bytes backing the integer, in big ending notation. * * @param value The input integer. * @return The array of bytes representing the integer. */ public static byte[] toByteArray(int value) { int intByteSize = 4; byte[] byteArray = new byte[intByteSize]; for (int i = 0; i < intByteSize; i++) { byteArray[i] = (byte) ((value) >>> ((intByteSize - 1 - i) * 8)); } return byteArray; } /** * Convert an array of longs to an array of bytes. Each long is split into * 8 bytes and the list of all composing bytes is returned. To ordering is preserved. * * @param longArray The input array of longs * @return An array of bytes. */ public static byte[] toByteArray(long[] longArray) { int longByteSize = 8; byte[] byteArray = new byte[longArray.length * longByteSize]; for (int i = 0; i < longArray.length; i++) { for (int j = 0; j < longByteSize; j++) { //to obtain each byte, shift the long from which it is take to the right //and truncate all leading bits by down-casting to byte. byteArray[i * longByteSize + j] = (byte) (longArray[i] >>> ((7 - j) * 8)); } } return byteArray; } }