Java Byte Array Create toBytes(int i)

Here you can find the source of toBytes(int i)

Description

to Bytes

License

Apache License

Declaration

public static byte[] toBytes(int i) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte[] toBytes(int i) {
        int shift = 8;
        byte[] buf = new byte[4];
        int pos = buf.length;
        int radix = 1 << shift;
        int mask = radix - 1;
        do {//from  w ww  .ja  v a 2  s. co  m
            buf[--pos] = (byte) (i & mask);
            i >>>= shift;
        } while (i != 0);

        byte[] result = new byte[buf.length - pos];

        System.arraycopy(buf, pos, result, 0, result.length);
        return result;

    }
}

Related

  1. toBytes(final String text)
  2. toBytes(int data)
  3. toBytes(int data)
  4. toBytes(int i)
  5. toBytes(int i)
  6. toBytes(int i)
  7. toBytes(int i)
  8. toBytes(int i)
  9. toBytes(int i)