Java Convert via ByteBuffer toByteArray(int value)

Here you can find the source of toByteArray(int value)

Description

Create a fixed-size 32-bit (4 byte) byte[] from an int value using a big endian byte order.

License

Apache License

Parameter

Parameter Description
value The value.

Return

The byte[].

Declaration

public static byte[] toByteArray(int value) 

Method Source Code


//package com.java2s;
/*//from   www  . j a v a  2  s .  c o  m
 * Licensed to the Warcraft4J Project under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The Warcraft4J Project licenses
 * this file to you 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.
 */

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.util.Optional;

public class Main {
    /**
     * Create a fixed-size 32-bit (4 byte) byte[] from an int value using a big endian byte order.
     *
     * @param value The value.
     *
     * @return The byte[].
     */
    public static byte[] toByteArray(int value) {
        return toByteArray(value, ByteOrder.BIG_ENDIAN);
    }

    /**
     * Create a fixed-size 32-bit (4 byte) byte[] from an int value using a byte order.
     *
     * @param value     The value.
     * @param byteOrder The byte order, using {@code ByteOrder#BIG_ENDIAN} when the byte order is {@code null}.
     *
     * @return The byte[].
     */
    public static byte[] toByteArray(int value, ByteOrder byteOrder) {
        byte[] intArray = new byte[Integer.BYTES];
        ByteBuffer.wrap(intArray).order(Optional.ofNullable(byteOrder).orElse(ByteOrder.BIG_ENDIAN)).putInt(value);
        return intArray;
    }

    /**
     * Create a fixed size 64-bit (8 byte) byte[] from the given long value using a big endian byte order.
     *
     * @param value The value.
     *
     * @return The byte[].
     */
    public static byte[] toByteArray(long value) {
        return toByteArray(value, ByteOrder.BIG_ENDIAN);
    }

    /**
     * Create a fixed-size 64-bit (8 byte) byte[] from an int value using a byte order.
     *
     * @param value     The value.
     * @param byteOrder The byte order, using {@code ByteOrder#BIG_ENDIAN} when the byte order is {@code null}.
     *
     * @return The byte[].
     */
    public static byte[] toByteArray(long value, ByteOrder byteOrder) {
        byte[] longArray = new byte[Long.BYTES];
        ByteBuffer.wrap(longArray).order(Optional.ofNullable(byteOrder).orElse(ByteOrder.BIG_ENDIAN))
                .putLong(value);
        return longArray;
    }
}

Related

  1. toByteArray(double[] doubleArray)
  2. toByteArray(float[] floatArray)
  3. toByteArray(InputStream input, boolean nioCopy)
  4. toByteArray(int i)
  5. toByteArray(int integer)
  6. toByteArray(int value)
  7. toByteArray(int[] data, boolean bigEndian)
  8. toByteArray(int[] intArray)
  9. toByteArray(long[] data)