Java Integer to Byte Array intToBytes(int v, final byte[] arr)

Here you can find the source of intToBytes(int v, final byte[] arr)

Description

Returns a Little-Endian byte array extracted from the given int.

License

Apache License

Parameter

Parameter Description
v the given int
arr a given array of 4 bytes that will be returned with the data

Return

a Little-Endian byte array extracted from the given int.

Declaration

public static byte[] intToBytes(int v, final byte[] arr) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  j  a  v  a 2 s . c o m*/
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Returns a Little-Endian byte array extracted from the given int.
     * @param v the given int
     * @param arr a given array of 4 bytes that will be returned with the data
     * @return a Little-Endian byte array extracted from the given int.
     */
    public static byte[] intToBytes(int v, final byte[] arr) {
        for (int i = 0; i < 4; i++) {
            arr[i] = (byte) (v & 0XFF);
            v >>>= 8;
        }
        return arr;
    }
}

Related

  1. intToBytes(int num, byte[] bytes, int startIndex)
  2. intToBytes(int number, byte[] destination, int destinationIndex)
  3. intToBytes(int v)
  4. intToBytes(int v)
  5. intToBytes(int v, byte[] bytes)
  6. intToBytes(int val)
  7. intToBytes(int val, byte[] bytes, int start)
  8. intToBytes(int val, int byteCount)
  9. intToBytes(int value)