Here you can find the source of longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
Parameter | Description |
---|---|
longValue | a parameter |
byteOrder | a parameter |
private static byte[] longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
//package com.java2s; //License from project: Apache License import java.nio.ByteOrder; public class Main { public final static int BITS_PER_BYTE = 8; private final static int BYTES_IN_A_LONG = 8; /**//from www.j a va2 s . co m * Convert the provided longValue to a byte array of size 8. * * @param longValue * @param byteOrder * @return */ private static byte[] longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned) { byte[] result = new byte[BYTES_IN_A_LONG]; if (longValue < 0 && !isSigned) { throw new IllegalStateException( "Cannot represent the negative value[" + longValue + "] in an unsigned byte array."); } if (byteOrder == ByteOrder.BIG_ENDIAN) { for (int i = BYTES_IN_A_LONG - 1; i >= 0; i--) { result[i] = (byte) (longValue & 0xFF); longValue >>= BITS_PER_BYTE; } } else if (byteOrder == ByteOrder.LITTLE_ENDIAN) { for (int i = 0; i < BYTES_IN_A_LONG; i++) { result[i] = (byte) (longValue & 0xFF); longValue >>= BITS_PER_BYTE; } } else { throw new IllegalStateException("Unrecognized ByteOrder value[" + byteOrder + "]."); } return result; } }