Skip to content

Commit 82dd073

Browse files
This commit is related to BAEL-7837 (#16827)
This commit aims to add a test class IntToUnsignedByteUnitTest.
1 parent 8035eeb commit 82dd073

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.inttounsignedbyte;
2+
3+
import org.junit.Test;
4+
5+
import java.nio.ByteBuffer;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class IntToUnsignedByteUnitTest {
10+
int value = 200;
11+
12+
@Test
13+
public void givenInt_whenUsingTypeCastingAndBitMasking_thenConvertToUnsignedByte() {
14+
byte unsignedByte = (byte) (value & 0xFF);
15+
16+
assertEquals(200, Byte.toUnsignedInt(unsignedByte));
17+
}
18+
19+
@Test
20+
public void givenIntInRange_whenUsingByteBuffer_thenConvertToUnsignedByte() {
21+
int value = 200;
22+
ByteBuffer buffer = ByteBuffer.allocate(4).putInt(value);
23+
byte unsignedByte = buffer.array()[3];
24+
25+
assertEquals(200, Byte.toUnsignedInt(unsignedByte));
26+
}
27+
}

0 commit comments

Comments
 (0)