Android Int to Byte Array Convert toByteArray(int in)

Here you can find the source of toByteArray(int in)

Description

to Byte Array

Declaration

public static byte[] toByteArray(int in) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] toByteArray(int in) {
        byte[] out = new byte[4];

        out[0] = (byte) in;
        out[1] = (byte) (in >> 8);
        out[2] = (byte) (in >> 16);
        out[3] = (byte) (in >> 24);

        return out;
    }//  ww  w .j a  va  2s  .c  o m

    public static byte[] toByteArray(int in, int outSize) {
        byte[] out = new byte[outSize];
        byte[] intArray = toByteArray(in);
        for (int i = 0; i < intArray.length && i < outSize; i++) {
            out[i] = intArray[i];
        }
        return out;
    }
}

Related

  1. uInt16ToBytesLI(int value, byte[] buffer, int startIndex)
  2. uInt32ToBytesBI(long value)
  3. uInt32ToBytesBI(long value, byte[] buffer, int startIndex)
  4. uInt32ToBytesLI(long value)
  5. uInt32ToBytesLI(long value, byte[] buffer, int startIndex)
  6. toByteArray(int in, int outSize)
  7. toByteArray(int val, byte[] b, int pos)
  8. ints2bytesBE(int[] val)
  9. ints2bytesLE(int[] val)