Int or long to binary, bytes, hexa and octal in Java

Java int or long to binary

Convert number to array of binary:

int i = 10;
System.out.println("The binary value of i is:" + Integer.toBinaryString(i));
long l = 6L;
System.out.println("The binary value of l is:" + Long.toBinaryString(l));

Java int to 4 bytes

Convert a number to 4 bytes:

//method 1
public static final byte[] intToByteArray(int value) {
    return new byte[] {
            (byte)(value >>> 24),
            (byte)(value >>> 16),
            (byte)(value >>> 8),
            (byte)value};
}
//method 2
BigInteger.valueOf(value).toByteArray()

Java int to hexa

Convert a number to hexadecimal:

Integer.toHexString(i)

Java int to octal

Convert a number to octal:

Integer.toOctalString(i)