集册 Java实例教程 将长数组转换为长度为8的字节数组。

将长数组转换为长度为8的字节数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

466
将长数组转换为长度为8的字节数组。
/**from 时代Java - nowjava.com**/


//package com.nowjava;


public class Main {

    public static void main(String[] argv) throws Exception {

        long a = 2;

        byte[] buf = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

        longToByteArray(a, buf);

    }


    /**

     * Converts an long to a byte array with a length of 8.

     * @param a - The long to convert.

     * @param buf - The byte array to store the integer in. This must be a length of at least 8.

     */

    public static void longToByteArray(long a, byte[] buf) {

        buf[0] = (byte) ((a >> 56) & 0xFF);

        buf[1] = (byte) ((a >> 48) & 0xFF);

        buf[2] = (byte) ((a >> 40) & 0xFF);

        buf[3] = (byte) ((a >> 32) & 0xFF);

        buf[4] = (byte) ((a >> 24) & 0xFF);

        bu
展开阅读全文