Java Long to Byte Array longToBytes(long data)

Here you can find the source of longToBytes(long data)

Description

Store a long as an array of bytes.

License

Apache License

Declaration

public static byte[] longToBytes(long data) 

Method Source Code

//package com.java2s;
/*//  ww w. ja  v a2  s.  c  om
 * Copyright (C) 2010 Indeed Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Store a long as an array of bytes.
     */
    public static byte[] longToBytes(long data) {
        final byte[] bytes = new byte[8];
        longToBytes(data, bytes, 0);
        return bytes;
    }

    /**
     * Store a long into an array of bytes.
     */
    public static void longToBytes(final long data, final byte[] buffer, final int offset) {
        buffer[offset + 0] = (byte) ((data >> 56) & 0xff);
        buffer[offset + 1] = (byte) ((data >> 48) & 0xff);
        buffer[offset + 2] = (byte) ((data >> 40) & 0xff);
        buffer[offset + 3] = (byte) ((data >> 32) & 0xff);
        buffer[offset + 4] = (byte) ((data >> 24) & 0xff);
        buffer[offset + 5] = (byte) ((data >> 16) & 0xff);
        buffer[offset + 6] = (byte) ((data >> 8) & 0xff);
        buffer[offset + 7] = (byte) ((data) & 0xff);
    }
}

Related

  1. longToBytes(final long aLong)
  2. longToBytes(final long l)
  3. longToBytes(final long l)
  4. longToBytes(final long val)
  5. longToBytes(final long value)
  6. longToBytes(long k, byte[] b, int i)
  7. longToBytes(long l)
  8. longToBytes(long l)
  9. longToBytes(long l)