Java Convert via ByteBuffer intToBytes(int i, byte[] backingStore, int offset)

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

Description

Convert a Java int to a 4-byte array.

License

Contributor Agreement License

Parameter

Parameter Description
i A Java int value.

Return

A 4-byte array representing the int value.

Declaration

public static byte[] intToBytes(int i, byte[] backingStore, int offset) 

Method Source Code


//package com.java2s;
/*//from  ww  w  . j  a va2s  . c om
 *  Copyright (C) 2008 Pingtel Corp., certain elements licensed under a Contributor Agreement.
 *  Contributors retain copyright to elements licensed under a Contributor Agreement.
 *  Licensed to the User under the LGPL license.
 *
 */

import java.nio.ByteBuffer;

public class Main {
    /** Number of bytes in a Java int. */
    public static final int NUM_BYTES_IN_INT = 4;

    /**
     * Convert a Java int to a 4-byte array.
     * 
     * @param i
     *            A Java int value.
     * @return A 4-byte array representing the int value.
     */
    public static byte[] intToBytes(int i, byte[] backingStore, int offset) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(NUM_BYTES_IN_INT);
        byteBuffer.putInt(i);
        return byteBuffer.array();

    }
}

Related

  1. intToByteArray(int someInt, int byteSize)
  2. intToBytes(final int i)
  3. intToBytes(final int integer)
  4. intToBytes(final int value)
  5. intToBytes(final int x)
  6. intToBytes(int n)
  7. intToBytes(int tagId)
  8. intToBytesLE(int value)
  9. intToBytesWithLen(int x, int len)