Java Integer to Byte Array intToBytes(int i)

Here you can find the source of intToBytes(int i)

Description

int To Bytes

License

Open Source License

Declaration

public static byte[] intToBytes(int i) 

Method Source Code

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

public class Main {
    public static final int NUMBER_OF_BITS_IN_A_BYTE = 8;
    public static final short MASK_TO_BYTE = 0xFF;

    public static byte[] intToBytes(int i) {
        byte[] bytes = new byte[4];
        /*//from  w w w. j ava 2 s  . c o  m
        bytes[0] = (byte)(i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[1] = (byte)(i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[2] = (byte)(i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[3] = (byte)(i & MASK_TO_BYTE);
        */

        bytes[3] = (byte) (i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[2] = (byte) (i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[1] = (byte) (i & MASK_TO_BYTE);
        i >>= NUMBER_OF_BITS_IN_A_BYTE;
        bytes[0] = (byte) (i & MASK_TO_BYTE);

        return bytes;
    }
}

Related

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