Convert Short to Byte Array
1. Introduction
Converting a short value to a byte array is a common task when dealing with binary data. In this example, I will demonstrate short byte array conversion in the following ways:
- Using java.nio.ByteBuffer‘s array method.
- Using java.io.ByteArrayOutputStream‘s toByteArray method.
- Using the signed right shift operator >>.
2. Setup
In this step, I will create a maven project with the Junit library.
pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >org.zheng.demo</ groupId > < artifactId >convert_short_to_bytearray</ artifactId > < version >0.0.1-SNAPSHOT</ version > < build > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.8.1</ version > < configuration > < release >17</ release > </ configuration > </ plugin > </ plugins > </ build > < dependencies > <!-- junit 5 --> < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter-engine</ artifactId > < version >5.5.2</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
3. Junit Test Class
In this step, I will convert a short value to a byte array with three tests.
TestConvertShortToByteArray.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package convert_short_to_bytearray; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import org.junit.jupiter.api.Test; public class TestConvertShortToByteArray { short shortValue = 32767 ; byte [] expectedByteArray = { 127 , - 1 }; @Test public void convertToByteArray_via_ByteBuffer() { byte [] byteArray = ByteBuffer.allocate(Short.BYTES).putShort(shortValue).array(); assertArrayEquals(expectedByteArray, byteArray); } @Test public void convertToByteArray_via_DataOutputStream() throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos)) { dos.writeShort(shortValue); byte [] byteArray = baos.toByteArray(); assertArrayEquals(expectedByteArray, byteArray); } } @Test public void convertToByteArray_via_BitShiftOperator() { byte [] byteArray = new byte [Short.BYTES]; byteArray[ 0 ] = ( byte ) (shortValue >> 8 ); byteArray[ 1 ] = ( byte ) shortValue; assertArrayEquals(expectedByteArray, byteArray); } } |
- line 18:
convertToByteArray_via_ByteBuffer
– utilizes thejava.nio.ByteBuffer
class. See step 3.1 for a detailed explanation. - line 25:
convertToByteArray_via_DataOutputStream
– usesjava.io.ByteArrayOutputStream
class. See step 3.2 for a detailed explanation. - line 37:
convertToByteArray_via_BitShiftOperator
– uses the signed right shift operator. See step 3.3 for a detailed explanation.
3.1 Short Byte Array Conversion via ByteBuffer
We will convert a short to a byte array with three steps.
- First, create a
ByteBuffer
object with the size ofShort.BYTES
viaByteBuffer.allocate(Short.BYTES)
- Second, write the given short value via
putShort(shortValue)
. - Last, invoke the
array
method to convert to a byte array.
3.2 Short Byte Array Conversion via ByteArrayOutputStream
Following three steps to convert a short into a byte array.
- First, create a
ByteArrayOutputStream
andDataOutputStream
objects. - Then write the short value via
dos.writeShort(shortValue)
. - Finally, convert to a byte array via
baos.toByteArray()
;
3.3 Short Byte Array Conversion via Bit Shift Operator
A short in Java is a 16-bit signed integer and takes 2
bytes in memory. The first byte stores the high-order bits and the second byte stores the lower-order bits. In this step, I will use the “signed right shift operator” ( >>)
to create a byte array from a short value.
- The
shortValue >> 8
shifts the bits ofshortValue
to the right by 8 positions. This operation effectively isolates the most significant byte of theshortValue
as the most significant byte (high-order byte) of a short is stored in the element at index 0 ofbyte[]
. - The least significant byte (low-order byte) is stored in the element at index 1 of
byte[]
. The(byte)
again castsshortValue
to abyte
, truncating the higher-order bits that do not fit into a byte.
4. Run Test
In this step, I will run the tests and verify the tests are passed.
5. Conclusion
In this example, I demonstrated short byte array conversion in the following ways:
- Using java.nio.ByteBuffer‘s array method.
- Using java.io.ByteArrayOutputStream‘s toByteArray method.
- Using the signed right shift operator >>.
6. Download
This was an example of a maven project which converts a short value into a byte array.
You can download the full source code of this example here: Convert Short to Byte Array