Java Byte Array Create toBytes(long n)

Here you can find the source of toBytes(long n)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(long n) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.//from w w w .j  a  va 2  s .  c o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

public class Main {
    public static byte[] toBytes(long n) {
        byte[] bytes = new byte[8];
        toBytes(bytes, 0, n);
        return bytes;
    }

    public static void toBytes(byte[] bytes, int startPos, long n) {
        for (int i = startPos + 7; i >= startPos; i--) {
            bytes[i] = (byte) n;
            n >>>= 8;
        }
    }

    public static void toBytes(byte[] bytes, int startPos, int n) {
        for (int i = startPos + 3; i >= startPos; i--) {
            bytes[i] = (byte) n;
            n >>>= 8;
        }
    }
}

Related

  1. toBytes(int[] ints)
  2. toBytes(long input)
  3. toBytes(long l)
  4. toBytes(long l, byte[] bytes, int offset, int limit)
  5. toBytes(long lp)
  6. toBytes(long number)
  7. toBytes(long size)
  8. toBytes(long v, byte[] writeBuffer, int o)
  9. toBytes(long val)