Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Converting a short value to a byte[] array is a common task in Java programming, especially when dealing with binary data or network communication.

In this tutorial, we’ll explore various approaches to achieve this conversion efficiently.

2. Using ByteBuffer Class (Java NIO)

The Java NIO package provides the ByteBuffer class, which simplifies the conversion of primitive data types into byte arrays. Let’s take a look at how we can use it to convert a short value to a byte[] array:

short shortValue = 12345;
byte[] expectedByteArray = {48, 57};

@Test
public void givenShort_whenUsingByteBuffer_thenConvertToByteArray() {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.putShort(shortValue);
    byte[] byteArray = buffer.array();

    assertArrayEquals(expectedByteArray, byteArray);
}

In this method, we use the allocate() method to allocate a ByteBuffer with a capacity of 2 bytes to accommodate shortValue. Next, we utilize the putShort() method to write the binary representation of shortValue into the buffer object. This operation results in the buffer containing the byte representation of shortValue.

We then extract the byte array named byteArray from the buffer using the array() method, which retrieves the byte array corresponding to the stored short value.

Finally, we assert that byteArray matches the expectedByteArray using the assertArrayEquals() method, ensuring the accuracy of the conversion process.

3. Using DataOutputStream Class

Another approach is to utilize the DataOutputStream class, which provides an efficient way to accomplish the conversion process. Let’s see how we can implement this approach:

@Test
public void givenShort_whenUsingDataOutputStream_thenConvertToByteArray() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeShort(shortValue);
    dos.close();
    byte[] byteArray = baos.toByteArray();
    assertArrayEquals(expectedByteArray, byteArray);
}

In this test method, we first utilize the DataOutputStream class to write the short value to a ByteArrayOutputStream object named baos.

Moreover, we invoke the writeShort() method to serialize shortValue into two bytes that represent its binary form. Subsequently, we retrieve the resulting byte array from baos using the toByteArray() method.

4. Manual Bit Manipulation

This approach effectively converts a short value into a byte array by explicitly manipulating the bits of the short value to isolate and store the most significant byte (MSB) and least significant byte (LSB) components in the respective positions of the byte array.

Let’s delve into the implementation:

@Test
public void givenShort_whenUsingManualBitManipulation_thenConvertToByteArray() {
    byte[] byteArray = new byte[2];
    byteArray[0] = (byte) (shortValue >> 8);
    byteArray[1] = (byte) shortValue;

    assertArrayEquals(expectedByteArray, byteArray);
}

Here, we first extract the MSB by shifting shortValue right by 8 bits (shortValue >> 8), and the result is cast to a byte to store in byteArray[0]. Similarly, the least significant byte (LSB) of shortValue is obtained by casting it directly to a byte and then storing it in byteArray[1].

5. Conclusion

In conclusion, mastering the conversion of a short value to a byte[] array in Java is essential for various tasks. Hence, we explored different approaches, such as using ByteBuffer from Java NIO, manual bit manipulation, or leveraging DataOutputStream.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW