Java Convert via ByteBuffer toArray(long length)

Here you can find the source of toArray(long length)

Description

to Array

License

Apache License

Declaration

public static byte[] toArray(long length) 

Method Source Code


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

import java.nio.ByteBuffer;

public class Main {
    public static byte[] toArray(long length) {
        long value = length;
        byte[] b = new byte[8];
        for (int i = 7; i >= 0 && value > 0; i--) {
            b[i] = (byte) (value & 0xFF);
            value >>= 8;/*  w  w  w.j a va2s.  c o m*/
        }
        return b;
    }

    public static byte[] toArray(int value) {
        ByteBuffer b = ByteBuffer.allocate(4);
        return b.putInt(value).array();
    }
}

Related

  1. stringToLongUnknownLength(String str, int startIndex)
  2. to32BitsLongArray(byte[] data, boolean bigEndian)
  3. toArray(final byte[] byteArray, final int sizeLimit)
  4. toArray(int value)
  5. toArray(IntBuffer src, int[] dst, int offset)
  6. toASCII(String str)
  7. toAsciiBytes(String s)
  8. toASCIIString(URI u)
  9. toAttributeValue(Object value)