Java Integer to Byte Array int2Bytes(int i, byte[] bytes, int offset)

Here you can find the source of int2Bytes(int i, byte[] bytes, int offset)

Description

int Bytes

License

Open Source License

Declaration

public static byte[] int2Bytes(int i, byte[] bytes, int offset) throws Exception 

Method Source Code

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

public class Main {
    public static byte[] int2Bytes(int i, byte[] bytes, int offset) throws Exception {
        if (bytes == null || bytes.length - offset < 4)
            throw new Exception("bytes==null||bytes.length-offset<4");

        bytes[0 + offset] = (byte) (i & 0xFF);
        bytes[1 + offset] = (byte) ((i >> 8) & 0xFF);
        bytes[2 + offset] = (byte) ((i >> 16) & 0xFF);
        bytes[3 + offset] = (byte) ((i >> 24) & 0xFF);
        return bytes;
    }//  w ww  . jav a2  s.c om

    public static byte[] int2Bytes(int i) throws Exception {
        byte[] bytes = new byte[4];
        int2Bytes(i, bytes, 0);
        return bytes;
    }
}

Related

  1. fromIntToByte(int integer)
  2. fromIntToByteArray(int value)
  3. int2Arr(int var, byte[] arrayBytes, int startIndex)
  4. int2ByteLE(byte[] bytes, int value, int offset)
  5. int2bytes(int i)
  6. int2bytes(int integer)
  7. int2bytes(int intValue)
  8. int2Bytes(int l, byte[] target, int offset)
  9. int2Bytes(int n, byte[] dst, int start)