Java Byte Array Create toBytes(int is)

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

Description

to Bytes

License

Open Source License

Declaration

public static final byte[] toBytes(int is) 

Method Source Code

//package com.java2s;

public class Main {

    public static final byte[] toBytes(int is) {
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            bytes[3 - i] = (byte) ((is >>> (i << 3)) & 0xFF);
        }/*from   w  ww  . j a  v a 2s . c  o m*/
        return bytes;
    }

    public static final byte[] toBytes(long l) {
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++) {
            bytes[7 - i] = (byte) ((l >>> (i << 3)) & 0xFF);
        }
        return bytes;
    }

    public static final byte[] toBytes(short s) {
        byte[] bytes = new byte[2];
        bytes[1] = (byte) (s & 0xFF);
        bytes[0] = (byte) (s >>> 8);
        return bytes;
    }
}

Related

  1. toBytes(int i)
  2. toBytes(int i)
  3. toBytes(int integer)
  4. toBytes(int integer)
  5. toBytes(int intValue)
  6. toBytes(int megabytes)
  7. toBytes(int n)
  8. toBytes(int n)
  9. toBytes(int val)