Java Integer to Byte Array intToBytes(int num, byte[] bytes, int startIndex)

Here you can find the source of intToBytes(int num, byte[] bytes, int startIndex)

Description

translate int into bytes, stored in byte array starting from startIndex

License

Open Source License

Parameter

Parameter Description
num the integer to be translated
bytes [] the byte array
startIndex starting to store in this index

Declaration

public static int intToBytes(int num, byte[] bytes, int startIndex) 

Method Source Code

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

public class Main {
    /**/*  w  w w.  j  av a 2  s  .  c o m*/
     * translate int into bytes, stored in byte array starting from startIndex
     * 
     * @param num
     *            the integer to be translated
     * @param bytes
     *            [] the byte array
     * @param startIndex
     *            starting to store in this index
     * @ret the index of the cell after storing the number.
     */
    public static int intToBytes(int num, byte[] bytes, int startIndex) {
        bytes[startIndex] = (byte) (num & 0xff);
        bytes[startIndex + 1] = (byte) ((num >> 8) & 0xff);
        bytes[startIndex + 2] = (byte) ((num >> 16) & 0xff);
        bytes[startIndex + 3] = (byte) ((num >> 24) & 0xff);
        return startIndex + 4;
    }
}

Related

  1. intToBytes(int intValue)
  2. intToBytes(int ipInt)
  3. intToBytes(int n)
  4. intToBytes(int n)
  5. intToBytes(int num, byte[] arr, int pos)
  6. intToBytes(int number, byte[] destination, int destinationIndex)
  7. intToBytes(int v)
  8. intToBytes(int v)
  9. intToBytes(int v, byte[] bytes)