Java Byte Array Create toByteArray(int num)

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

Description

to Byte Array

License

Apache License

Declaration

static byte[] toByteArray(int num) 

Method Source Code

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

public class Main {
    static byte[] toByteArray(int num) {
        final int INT_BYTES = Integer.SIZE / 8;

        byte[] bytes = new byte[INT_BYTES];
        for (int i = 0; i < INT_BYTES; i++) {
            bytes[(INT_BYTES - 1) - i] = (byte) (num >>> 8 * i);
        }//from w w w.j  a  v  a 2 s . co  m

        return bytes;
    }

    static byte[] toByteArray(long num) {
        final int LONG_BYTES = Long.SIZE / 8;

        byte[] bytes = new byte[LONG_BYTES];
        for (int i = 0; i < LONG_BYTES; i++) {
            bytes[(LONG_BYTES - 1) - i] = (byte) (num >>> 8 * i);
        }

        return bytes;
    }
}

Related

  1. toByteArray(int data)
  2. toByteArray(int i)
  3. toByteArray(int in)
  4. toByteArray(int in, int size)
  5. toByteArray(int intr)
  6. toByteArray(int value)
  7. toByteArray(int value)
  8. toByteArray(int value)
  9. toByteArray(int value)