Home > Java > javaTutorial > How to Convert a Long Value to a Byte Array and Back in Java?

How to Convert a Long Value to a Byte Array and Back in Java?

Mary-Kate Olsen
Release: 2024-11-04 21:08:01
Original
309 people have browsed it

How to Convert a Long Value to a Byte Array and Back in Java?

Converting Long to Byte Array and Back in Java

In Java, converting a long primitive data type to a byte array (byte[]) and vice versa is a common task for various operations, such as sending data over a TCP connection. Here's a comprehensive solution to achieve this conversion:

Long to Byte Array

<code class="java">public byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.putLong(x);
    return buffer.array();
}</code>
Copy after login

This method uses the ByteBuffer class to create a buffer of size Long.BYTES, which is the number of bytes required to represent a long value. The putLong() method is used to write the long value into the buffer, and the array() method returns the underlying byte array representing the long.

Byte Array to Long

<code class="java">public long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.put(bytes);
    buffer.flip(); // Flip the buffer to prepare for reading
    return buffer.getLong();
}</code>
Copy after login

To convert the byte array back to a long, a new ByteBuffer is created and loaded with the given byte array. The flip() method is used to make the buffer ready for reading. Finally, the getLong() method reads the long value from the buffer.

Encapsulation in a Helper Class

For convenience, the conversion methods can be encapsulated in a utility class:

<code class="java">public class ByteUtils {
    private static ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);

    public static byte[] longToBytes(long x) {
        buffer.putLong(0, x);
        return buffer.array();
    }

    public static long bytesToLong(byte[] bytes) {
        buffer.put(bytes, 0, bytes.length);
        buffer.flip(); // Flip the buffer to prepare for reading
        return buffer.getLong();
    }
}</code>
Copy after login

This utility class provides a simple way to perform the conversion without having to create and manage ByteBuffer instances each time.

Endianness Considerations

Note that the ByteBuffer class uses the native endianness of the system. If cross-platform compatibility is required, additional considerations for handling endianness may be necessary.

Alternative Solution: Using Libraries

While the native Java solutions provided above are adequate, they can become tedious in certain scenarios. For complex or extended data conversion needs, consider using a library like Guava or Apache Commons, which provide more comprehensive and efficient solutions.

The above is the detailed content of How to Convert a Long Value to a Byte Array and Back in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template