Java Integer to int2buff(int n)

Here you can find the source of int2buff(int n)

Description

int convert to buff (big-endian)

License

LGPL

Parameter

Parameter Description
n number

Return

4 bytes buff

Declaration

public static byte[] int2buff(int n) 

Method Source Code

//package com.java2s;
/**//  w  ww  . j  a v  a  2 s.  c om
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDHT Java Client may be copied only under the terms of the GNU Lesser
* General Public License (LGPL).
* Please visit the FastDHT Home Page http://fastdht.csource.org/ for more detail.
**/

public class Main {
    /**
    * int convert to buff (big-endian)
    * @param n number
    * @return 4 bytes buff
    */
    public static byte[] int2buff(int n) {
        byte[] bs;

        bs = new byte[4];
        bs[0] = (byte) ((n >> 24) & 0xFF);
        bs[1] = (byte) ((n >> 16) & 0xFF);
        bs[2] = (byte) ((n >> 8) & 0xFF);
        bs[3] = (byte) (n & 0xFF);

        return bs;
    }

    /**
    * int convert to buff (big-endian)
    * @param n number
    * @param bs byte buff
    * @param offset buff start index
    */
    public static void int2buff(int n, byte[] bs, int offset) {
        bs[offset] = (byte) ((n >> 24) & 0xFF);
        bs[offset + 1] = (byte) ((n >> 16) & 0xFF);
        bs[offset + 2] = (byte) ((n >> 8) & 0xFF);
        bs[offset + 3] = (byte) (n & 0xFF);

        return;
    }
}

Related

  1. int2(StringBuilder buf, int i)
  2. int2ABC(int index)
  3. int2array(int sz, int seed)
  4. int2ba(int integer)
  5. int2BigEndianStr(int i)
  6. int2Char(int i)
  7. int2Date(Integer date, String interval)
  8. int2ddouble(final int i)
  9. int2double(int[] ia)