Java Byte Array Create toBytesBigEndian(long value, int sizeInByte)

Here you can find the source of toBytesBigEndian(long value, int sizeInByte)

Description

Convert int/long to n-byte array.

License

Open Source License

Parameter

Parameter Description
value int/long value.
sizeInByte Size of byte array in byte.

Return

int/long as big-endian byte array of size sizeInByte .

Declaration

public static byte[] toBytesBigEndian(long value, int sizeInByte) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w ww  .  jav a2s  .c  o m*/
     * Convert int/long to n-byte array.
     *
     * @param value      int/long value.
     * @param sizeInByte Size of byte array in byte.
     * @return int/long as big-endian byte array of size {@code sizeInByte}.
     */
    public static byte[] toBytesBigEndian(long value, int sizeInByte) {
        byte[] out = new byte[sizeInByte];
        for (int i = (sizeInByte - 1); i >= 0; i--) {
            out[i] = (byte) value;
            value >>>= 8;
        }
        return out;
    }
}

Related

  1. toBytes(String s, int len, byte pad)
  2. toBytes(String str, byte[] bytes, int index)
  3. toBytes4(int n)
  4. toBytes4HexString(String str, char... separateds)
  5. toBytesBE(long v)
  6. toBytesBinary(String in)
  7. toBytesDirect(final String singleOctets)
  8. toBytesFromASCII(final char[] chars)
  9. toBytesFromBase64(String inBase64String)