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 step = 8;
        for (; (i >>> step) > 0 && step < 32; step += 8)
            ;/*  w  w w. j a v  a 2  s  .com*/
        byte[] b = new byte[step / 8];
        for (int x = 0, move = step - 8; x < (step / 8); x++, move -= 8) {
            b[x] = (byte) (i >>> move);
        }
        return b;
    }

    public static byte[] toBytes(long l) {
        int step = 8;
        for (; (l >>> step) > 0 && step < 64; step += 8)
            ;
        byte[] b = new byte[step / 8];
        for (int x = 0, move = step - 8; x < (step / 8); x++, move -= 8) {
            b[x] = (byte) (l >>> move);
        }
        return b;
    }
}

Related

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