convert int to byte array via ByteBuffer - Android java.lang

Android examples for java.lang:array convert

Description

convert int to byte array via ByteBuffer

Demo Code

import java.nio.ByteBuffer;

public class Main {

  /**// w w w.  java2 s.  c o m
   * convert int to byte array
   * 
   * @param value
   *          integer value
   * @return byte array
   */
  public static byte[] convertIntToByte(int value) {
    byte[] temp = new byte[4];
    byte[] result = ByteBuffer.allocate(4).putInt(value).array();
    temp[0] = result[3];
    temp[1] = result[2];
    temp[2] = result[1];
    temp[3] = result[0];
    return temp;
  }

}

Related Tutorials