Java Integer to Byte Array intToBytes(final int number)

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

Description

TODO: Documentation

License

Open Source License

Declaration

public static byte[] intToBytes(final int number) 

Method Source Code

//package com.java2s;

public class Main {
    /** TODO: Documentation */
    public static byte[] intToBytes(final int number) {
        return new byte[] { (byte) (number >> 24 & 0xFF), (byte) (number >> 16 & 0xFF), (byte) (number >> 8 & 0xFF),
                (byte) (number & 0xFF) };
    }/*from  w  w  w.  j  a v  a2  s .c om*/

    /** TODO: Documentation */
    public static byte[] intToBytes(final int number, final int arrayLength) {
        final byte[] bytes = new byte[arrayLength];
        int shifted = arrayLength * 8 - 8;
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) (number >> shifted & 0xFF);
            shifted -= 8;
        }
        return bytes;
    }
}

Related

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