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

How to Convert a Byte Array to a Long in Java?

Barbara Streisand
Release: 2024-11-05 18:11:02
Original
387 people have browsed it

How to Convert a Byte Array to a Long in Java?

Byte Array to Long Conversion in Java: A Comprehensive Guide

Converting between long data types and byte arrays is a fundamental task in Java programming, especially when exchanging data over networks or storing it in binary formats. This article outlines the various methods for performing this conversion effectively.

Long to Byte Array Conversion

To convert a long value into a byte array, you can use the following approach:

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

In this code, a ByteBuffer object is allocated with a size equal to the number of bytes required to represent a long value. The putLong method is used to write the long value into the buffer, and finally, the array method retrieves the underlying byte array representation.

Byte Array to Long Conversion

To perform the reverse conversion, you can use the following code:

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

Here, a ByteBuffer object is again used, this time to wrap around the provided byte array. The put method is used to copy the bytes into the buffer, followed by flipping the buffer to indicate that it's now ready for reading. Finally, the getLong method retrieves the long value from the buffer.

Avoiding Repeated ByteBuffer Creation

For scenarios where multiple conversions will be performed, it's more efficient to avoid creating new ByteBuffer objects for each conversion. This can be achieved by wrapping the process in a 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();
        return buffer.getLong();
    }
}</code>
Copy after login

In this case, a single ByteBuffer object is shared across all conversions, minimizing the overhead of object creation.

Conclusion

The methods described in this article provide efficient and reliable ways to convert between long values and byte arrays in Java. Whether you opt for the directByteBuffer approach or utilize the ByteUtils wrapper, you can confidently handle these conversions in your applications.

The above is the detailed content of How to Convert a Byte Array to a Long 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