Java Integer Convert To fromInt(int input)

Here you can find the source of fromInt(int input)

Description

Returns a byte array containing 4 big-endian ordered bytes representing the given integer.

License

Apache License

Parameter

Parameter Description
input The integer to convert to a byte array.

Return

A byte array representation of an integer.

Declaration

public static byte[] fromInt(int input) 

Method Source Code

//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);
    }
}

Related

  1. convertIntToUsignedBytes(int integer)
  2. fromInt(byte[] buffer, int pos, int i)
  3. fromInt(final int value, final int length)
  4. fromInt(int i)
  5. fromint(int i)
  6. fromInt(int input)
  7. fromInt(int intValue)
  8. fromInt(int key)
  9. fromInt(int value)