Converting Between Byte Arrays and Short Arrays in Java
This article demonstrates how to convert a byte array to a short array and convert a short array back to a byte array in Java using the ByteBuffer class. The approach shown here provides an efficient way to handle binary data transformation while ensuring correct byte ordering and avoiding manual bit manipulation.
1. Understanding the Conversion Approach
A byte represents eight bits, while a short represents sixteen bits. This means each short value is formed from two consecutive bytes. The ByteBuffer class in Java simplifies this conversion by allowing a byte array to be viewed as a short buffer. This enables direct reading and writing of short values without manual shifting operations. The correct byte order must be specified to ensure consistent results across different systems.
2. Converting Byte Array to Short Array
The code below shows how to convert a byte array into a short array using ByteBuffer with little endian ordering.
public class ByteToShortConverter {
public static short[] convertBytesToShorts(byte[] inputBytes) {
if (inputBytes == null || inputBytes.length % 2 != 0) {
throw new IllegalArgumentException("Input byte array must not be null and must have even length");
}
short[] outputShorts = new short[inputBytes.length / 2];
ByteBuffer.wrap(inputBytes)
.order(ByteOrder.LITTLE_ENDIAN)
.asShortBuffer()
.get(outputShorts);
return outputShorts;
}
}
The code defines a utility method to convert a byte array to a short array. It validates that the input is not null and that its length is even, since each short is 2 bytes. A ByteBuffer is created by wrapping the input array and setting the byte order to LITTLE_ENDIAN. The buffer is then viewed as a short buffer, which allows the data to be directly read into a short array.
3. Converting Short Array to Byte Array
This section shows how to convert a short array back into a byte array using ByteBuffer with the same byte order.
public class ShortToByteConverter {
public static byte[] convertShortsToBytes(short[] inputShorts) {
if (inputShorts == null) {
throw new IllegalArgumentException("Input short array must not be null");
}
byte[] outputBytes = new byte[inputShorts.length * 2];
ByteBuffer.wrap(outputBytes)
.order(ByteOrder.LITTLE_ENDIAN)
.asShortBuffer()
.put(inputShorts);
return outputBytes;
}
}
The method converts a short array into a byte array by first allocating a byte array that is twice the size of the short array. A ByteBuffer wraps the output array and is configured to use little-endian ordering. The short buffer view of the ByteBuffer allows the short values to be written directly into the underlying byte array. This ensures that each short is correctly split into two bytes.
Below is an application that demonstrates both conversion directions and verifies the results.
public class Application {
public static void main(String[] args) {
byte[] initialBytes = new byte[]{0x01, 0x00, 0x02, 0x00, 0x03, 0x00};
short[] convertedShorts = ByteToShortConverter.convertBytesToShorts(initialBytes);
byte[] finalBytes = ShortToByteConverter.convertShortsToBytes(convertedShorts);
IO.println("Initial byte array: " + Arrays.toString(initialBytes));
IO.println("Converted short array: " + Arrays.toString(convertedShorts));
IO.println("Final byte array: " + Arrays.toString(finalBytes));
}
}
Output
Initial byte array: [1, 0, 2, 0, 3, 0] Converted short array: [1, 2, 3] Final byte array: [1, 0, 2, 0, 3, 0]
The printed output confirms that the original byte array and the final reconstructed byte array are identical, demonstrating that the conversion process preserves data integrity.
4. Endianness Considerations
Endianness defines the order in which bytes are interpreted when forming larger data types. This implementation uses little-endian order, meaning the least significant byte is processed first. Both conversion methods must use the same byte order to ensure consistent and correct results. Changing the byte order requires updating both methods accordingly.
5. Conclusion
In this article, the process of converting between byte arrays and short arrays in Java was demonstrated using the ByteBuffer class. The examples showed how to convert a byte array into a short array and how to reverse the process back into a byte array while maintaining data integrity.
6. Download the Source Code
This article explained how to convert between byte arrays and short arrays in Java.
You can download the full source code of this example here: convert byte to short array in Java

