May 16

The following code can be used to extract the 8 bytes from a long value and return them as a byte array

public static byte[] longToByteArray(long data) {
    return new byte[] {
        (byte)((data >> 56) & 0xff),
        (byte)((data >> 48) & 0xff),
        (byte)((data >> 40) & 0xff),
        (byte)((data >> 32) & 0xff),
        (byte)((data >> 24) & 0xff),
        (byte)((data >> 16) & 0xff),
        (byte)((data >> 8 ) & 0xff),
        (byte)((data >> 0) & 0xff),
    };
}

written by objects \\ tags: , , , ,

One Ping to “How to convert a long to a byte array”

  1. How to convert byte array to long | web development helpdesk Says:

    […] is the reverse of “How to convert a long to a byte array“. AKPC_IDS += […]


Leave a Reply

You must be logged in to post a comment.