Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.baeldung.endianconversion;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.stream.IntStream;

public final class ByteShortConverter {

private ByteShortConverter() {
}

public static byte[] shortsToBytesBigEndian(short[] shorts) {
ByteBuffer buffer = ByteBuffer.allocate(shorts.length * 2).order(ByteOrder.BIG_ENDIAN);
buffer.asShortBuffer().put(shorts);
return buffer.array();
}

public static byte[] shortsToBytesBigEndianUsingLoop(short[] shorts) {
byte[] bytes = new byte[shorts.length * 2];
for (int i = 0; i < shorts.length; i++) {
short value = shorts[i];
bytes[2 * i] = (byte) ((value >>> 8) & 0xFF);
bytes[2 * i + 1] = (byte) (value & 0xFF);
}
return bytes;
}

public static byte[] shortsToBytesLittleEndian(short[] shorts) {
ByteBuffer buffer = ByteBuffer.allocate(shorts.length * 2).order(ByteOrder.LITTLE_ENDIAN);
buffer.asShortBuffer().put(shorts);
return buffer.array();
}

public static byte[] shortsToBytesLittleEndianUsingLoop(short[] shorts) {
byte[] bytes = new byte[shorts.length * 2];
for (int i = 0; i < shorts.length; i++) {
short value = shorts[i];
bytes[2 * i] = (byte) (value & 0xFF);
bytes[2 * i + 1] = (byte) ((value >>> 8) & 0xFF);
}
return bytes;
}

public static short[] bytesToShortsBigEndian(byte[] bytes) {
short[] shorts = new short[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(shorts);
return shorts;
}

public static short[] bytesToShortsBigEndianUsingLoop(byte[] bytes) {
int n = bytes.length / 2;
short[] shorts = new short[n];
for (int i = 0; i < n; i++) {
shorts[i] = (short) (((bytes[2 * i] & 0xFF) << 8) | (bytes[2 * i + 1] & 0xFF));
}
return shorts;
}

public static short[] bytesToShortsBigEndianUsingStream(byte[] bytes) {
int n = bytes.length / 2;
short[] shorts = new short[n];
IntStream.range(0, n).forEach(i ->
shorts[i] = (short) (((bytes[2 * i] & 0xFF) << 8) | (bytes[2 * i + 1] & 0xFF)));
return shorts;
}

public static short[] bytesToShortsLittleEndianUsingLoop(byte[] bytes) {
int n = bytes.length / 2;
short[] shorts = new short[n];
for (int i = 0; i < n; i++) {
shorts[i] = (short) ((bytes[2 * i] & 0xFF) | ((bytes[2 * i + 1] & 0xFF) << 8));
}
return shorts;
}

public static short[] bytesToShortsLittleEndian(byte[] bytes) {
short[] shorts = new short[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
return shorts;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.baeldung.endianconversion;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class ByteShortConverterUnitTest {

@Test
void givenBytes_whenBytesToShortsBigEndian_thenCorrectValues() {
byte[] bytes = new byte[] { 0x7F, 0x1B, 0x10, 0x11 };
short[] shorts = ByteShortConverter.bytesToShortsBigEndian(bytes);

assertEquals(2, shorts.length);
assertEquals(32539, shorts[0]);
assertEquals(4113, shorts[1]);
}

@Test
void givenBytes_whenBytesToShortsLittleEndian_thenCorrectValues() {
byte[] bytes = new byte[] { 0x1B, 0x7F, 0x11, 0x10 };
short[] shorts = ByteShortConverter.bytesToShortsLittleEndian(bytes);

assertEquals(2, shorts.length);
assertEquals(32539, shorts[0]);
assertEquals(4113, shorts[1]);
}

@Test
void givenEmptyBytes_whenBytesToShorts_thenEmptyResult() {
byte[] emptyBytes = new byte[0];
short[] shorts = ByteShortConverter.bytesToShortsBigEndian(emptyBytes);
assertEquals(0, shorts.length);
}

@Test
void givenBigEndianBytes_whenBytesToShortsBigEndianUsingLoop_thenSameAsByteBuffer() {
byte[] bytes = new byte[] { 0x7F, 0x1B, 0x10, 0x11 };
short[] loopResult = ByteShortConverter.bytesToShortsBigEndianUsingLoop(bytes);
short[] bufferResult = ByteShortConverter.bytesToShortsBigEndian(bytes);
assertArrayEquals(bufferResult, loopResult);
}

@Test
void givenBigEndianBytes_whenBytesToShortsBigEndianUsingStream_thenSameAsByteBuffer() {
byte[] bytes = new byte[] { 0x7F, 0x1B, 0x10, 0x11 };
short[] streamResult = ByteShortConverter.bytesToShortsBigEndianUsingStream(bytes);
short[] bufferResult = ByteShortConverter.bytesToShortsBigEndian(bytes);
assertArrayEquals(bufferResult, streamResult);
}

@Test
void givenLittleEndianBytes_whenBytesToShortsLittleEndianUsingLoop_thenSameAsByteBuffer() {
byte[] bytes = new byte[] { 0x1B, 0x7F, 0x11, 0x10 };
short[] loopResult = ByteShortConverter.bytesToShortsLittleEndianUsingLoop(bytes);
short[] bufferResult = ByteShortConverter.bytesToShortsLittleEndian(bytes);
assertArrayEquals(bufferResult, loopResult);
}

@Test
void givenNegativeBytes_whenBytesToShortsBigEndian_thenCorrectValues() {
byte[] bytes = new byte[] { (byte) 0x80, 0x00, (byte) 0xFF, (byte) 0xFE };
short[] shorts = ByteShortConverter.bytesToShortsBigEndian(bytes);
assertEquals((short) 0x8000, shorts[0]);
assertEquals((short) 0xFFFE, shorts[1]);
}

@Test
void givenShorts_whenShortsToBytesBigEndian_thenCorrectByteOrder() {
short[] shorts = new short[] { (short) 0x7F1B, (short) 0x1011 };
byte[] bytes = ByteShortConverter.shortsToBytesBigEndian(shorts);
assertArrayEquals(new byte[] { 0x7F, 0x1B, 0x10, 0x11 }, bytes);
}

@Test
void givenShorts_whenShortsToBytesLittleEndian_thenCorrectByteOrder() {
short[] shorts = new short[] { (short) 0x7F1B, (short) 0x1011 };
byte[] bytes = ByteShortConverter.shortsToBytesLittleEndian(shorts);
assertArrayEquals(new byte[] { 0x1B, 0x7F, 0x11, 0x10 }, bytes);
}

@Test
void givenEmptyShorts_whenShortsToBytes_thenEmptyResult() {
short[] emptyShorts = new short[0];
assertArrayEquals(new byte[0], ByteShortConverter.shortsToBytesBigEndian(emptyShorts));
assertArrayEquals(new byte[0], ByteShortConverter.shortsToBytesLittleEndian(emptyShorts));
}

@Test
void givenBigEndianShorts_whenShortsToBytesBigEndianUsingLoop_thenSameAsByteBuffer() {
short[] shorts = new short[] { (short) 0x7F1B, (short) 0x1011, (short) 0x8000, (short) 0xFFFE };
byte[] loopResult = ByteShortConverter.shortsToBytesBigEndianUsingLoop(shorts);
byte[] bufferResult = ByteShortConverter.shortsToBytesBigEndian(shorts);
assertArrayEquals(bufferResult, loopResult);
}

@Test
void givenLittleEndianShorts_whenShortsToBytesLittleEndianUsingLoop_thenSameAsByteBuffer() {
short[] shorts = new short[] { (short) 0x7F1B, (short) 0x1011, (short) 0x8000, (short) 0xFFFE };
byte[] loopResult = ByteShortConverter.shortsToBytesLittleEndianUsingLoop(shorts);
byte[] bufferResult = ByteShortConverter.shortsToBytesLittleEndian(shorts);
assertArrayEquals(bufferResult, loopResult);
}

@Test
void givenShorts_whenShortsToBytesAndBack_thenRoundTrips() {
short[] shorts = new short[] { (short) 0x7F1B, (short) 0x1011, (short) 0x8000, (short) 0xFFFE };

byte[] bigEndianBytes = ByteShortConverter.shortsToBytesBigEndianUsingLoop(shorts);
assertArrayEquals(shorts, ByteShortConverter.bytesToShortsBigEndianUsingLoop(bigEndianBytes));

byte[] littleEndianBytes = ByteShortConverter.shortsToBytesLittleEndianUsingLoop(shorts);
assertArrayEquals(shorts, ByteShortConverter.bytesToShortsLittleEndianUsingLoop(littleEndianBytes));
}
}