Java Byte Array Create toBytes(int x, byte[] out)

Here you can find the source of toBytes(int x, byte[] out)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(int x, byte[] out) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] toBytes(int x, byte[] out) {
        byte[] intBytes = out == null ? new byte[4] : out;
        intBytes[3] = (byte) (x >> 24);
        intBytes[2] = (byte) (x >> 16);
        intBytes[1] = (byte) (x >> 8);
        intBytes[0] = (byte) (x >> 0);
        return intBytes;
    }//from   www.j ava  2  s.  c  o  m

    public static void toBytes(int[] src, int srcOffset, byte[] dst,
            int dstOffset, int length) {
        if (length < 0)
            throw new IllegalArgumentException(
                    "length must be >= 0, length = " + length);
        byte[] intBytes = null;

        for (int i = srcOffset; i < srcOffset + length; i++) {
            intBytes = toBytes(src[i], intBytes);

            dst[dstOffset++] = intBytes[0];
            dst[dstOffset++] = intBytes[1];
            dst[dstOffset++] = intBytes[2];
            dst[dstOffset++] = intBytes[3];
        }
    }
}

Related

  1. toBytes(int value)
  2. toBytes(int value)
  3. toBytes(int value)
  4. toBytes(int value)
  5. toBytes(int value, byte[] dest, int destPos)
  6. toBytes(int[] ints)
  7. toBytes(long input)
  8. toBytes(long l)
  9. toBytes(long l, byte[] bytes, int offset, int limit)