Java Integer to Byte Array intToBytes(final int value)

Here you can find the source of intToBytes(final int value)

Description

int To Bytes

License

LGPL

Declaration

public static byte[] intToBytes(final int value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static byte[] intToBytes(final int value) {
        final byte[] bytes = new byte[4];
        intToBytes(value, bytes, 0);//from   w  ww. j a  va2 s. co  m
        return bytes;
    }

    public static void intToBytes(final int value, final byte[] bytes, final int index) {
        assertNotNull("bytes", bytes);
        if (bytes.length - index < 4)
            throw new IllegalArgumentException("bytes.length - index < 4");

        for (int i = 0; i < 4; ++i)
            bytes[index + i] = (byte) (value >>> (8 * (4 - 1 - i)));
    }

    public static final <T> T assertNotNull(final String name, final T object) {
        if (object == null)
            throw new IllegalArgumentException(String.format("%s == null", name));

        return object;
    }
}

Related

  1. intToBytes(final int aInt)
  2. intToBytes(final int i)
  3. intToBytes(final int integer, final int bLength)
  4. intToBytes(final int number)
  5. intTobytes(final int value)
  6. intToBytes(int a, byte[] b, int bo)
  7. intToBytes(int i)
  8. intToBytes(int i)
  9. intToBytes(int i)