Here you can find the source of fromInt(int input)
Parameter | Description |
---|---|
input | The integer to convert to a byte array. |
public static byte[] fromInt(int input)
//package com.java2s; /*/*from ww w.jav a 2 s .c o m*/ * Copyright 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Returns a byte array containing 4 big-endian ordered bytes representing the * given integer. * * @param input The integer to convert to a byte array. * @return A byte array representation of an integer. */ public static byte[] fromInt(int input) { byte[] output = new byte[4]; writeInt(input, output, 0); return output; } /** * Writes 4 big-endian ordered bytes representing the given integer into the * destination byte array starting from the given offset. * * This method does not check the destination array length. * * @param input The integer to convert to bytes * @param dest The array in which to write the integer byte representation * @param offset The offset to start writing the bytes from */ static void writeInt(int input, byte[] dest, int offset) { dest[offset++] = (byte) (input >> 24); dest[offset++] = (byte) (input >> 16); dest[offset++] = (byte) (input >> 8); dest[offset++] = (byte) (input); } }