Java Byte Array Create toByteArray(long value)

Here you can find the source of toByteArray(long value)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(long value) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] toByteArray(long value) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            int offset = (8 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }//w ww.  j  av  a 2  s  .  co  m
        return b;
    }

    public static byte[] toByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (4 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }
        return b;
    }

    public static byte[] toByteArray(short value) {
        byte[] b = new byte[2];
        for (int i = 0; i < 2; i++) {
            int offset = (2 - 1 - i) * 8;
            b[i] = (byte) (value >>> offset & 0xFF);
        }
        return b;
    }
}

Related

  1. toByteArray(int[] data)
  2. toByteArray(int[] data, boolean includeLength)
  3. toByteArray(long hi, long lo)
  4. toByteArray(Long mac)
  5. toByteArray(long value)
  6. toByteArray(Number[] array)
  7. toByteArray(Object obj)
  8. toByteArray(Object value)
  9. toByteArray(short foo)