Java Integer to Byte Array intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian)

Here you can find the source of intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian)

Description

Converts an int to a sequence of bytes of the specified length and with the specified byte order.

License

Open Source License

Parameter

Parameter Description
value the value that is to be converted.
buffer the byte array that is to contain the byte sequence.
offset the offset to buffer at which the sequence is to start.
length the required length of the byte sequence.
littleEndian true if the byte order of the byte sequence is to be little-endian; false if the byte order is to be big-endian.

Declaration


protected static void intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*w  w w  .  java 2 s  .  c  o  m*/
     * Converts an {@code int} to a sequence of bytes of the specified length and with the specified byte
     * order.
     *
     * @param value         the value that is to be converted.
     * @param buffer        the byte array that is to contain the byte sequence.
     * @param offset        the offset to {@code buffer} at which the sequence is to start.
     * @param length        the required length of the byte sequence.
     * @param littleEndian  {@code true} if the byte order of the byte sequence is to be little-endian;
     *                      {@code false} if the byte order is to be big-endian.
     * @since 1.0
     * @see   #bytesToInt(byte[], int, int, boolean)
     * @see   #longToBytes(long, byte[], int, int, boolean)
     */

    protected static void intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian) {
        if (littleEndian)
            intToBytesLE(value, buffer, offset, length);
        else
            intToBytesBE(value, buffer, offset, length);
    }

    /**
     * Converts an {@code int} to a little-endian sequence of bytes of the specified length.
     *
     * @param value   the value that is to be converted.
     * @param buffer  the byte array that is to contain the byte sequence.
     * @param offset  the offset to {@code buffer} at which the sequence is to start.
     * @param length  the required length of the byte sequence.
     * @since 1.0
     * @see   #intToBytesBE(int, byte[], int, int)
     * @see   #bytesToIntLE(byte[], int, int, boolean)
     */

    private static void intToBytesLE(int value, byte[] buffer, int offset, int length) {
        int endOffset = offset + length;
        for (int i = offset; i < endOffset; ++i) {
            buffer[i] = (byte) value;
            value >>>= 8;
        }
    }

    /**
     * Converts an {@code int} to a big-endian sequence of bytes of the specified length.
     *
     * @param value   the value that is to be converted.
     * @param buffer  the byte array that is to contain the byte sequence.
     * @param offset  the offset to {@code buffer} at which the sequence is to start.
     * @param length  the required length of the byte sequence.
     * @since 1.0
     * @see   #intToBytesLE(int, byte[], int, int)
     * @see   #bytesToIntBE(byte[], int, int, boolean)
     */

    private static void intToBytesBE(int value, byte[] buffer, int offset, int length) {
        for (int i = offset + length - 1; i >= offset; --i) {
            buffer[i] = (byte) value;
            value >>>= 8;
        }
    }
}

Related

  1. intToBytes(int value)
  2. intToBytes(int value)
  3. intToBytes(int value)
  4. intToBytes(int value, byte[] array, int offset)
  5. intToBytes(int value, byte[] buffer, int offset)
  6. intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
  7. IntToBytes4(int i)
  8. intToBytesBE(int value, byte[] buffer)
  9. intToBytesBigend(int value)