This commit is related to the article BAEL-6988 (#15149)

This commit aims to add a test class "HexToIntConversionUnitTest.java" that provides several ways to convert Hex string into int.
This commit is contained in:
MohamedHelmyKassab 2023-11-06 19:40:42 +02:00 committed by GitHub
parent 946f849767
commit 4d7e5eb5c9
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.hextoint;
import org.junit.Test;
import java.math.BigInteger;
import static org.junit.Assert.assertEquals;
public class HexToIntConversionUnitTest {
@Test
public void givenValidHexString_whenUsingParseInt_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
int decimalValue = Integer.parseInt(hexString.substring(2), 16);
assertEquals(expectedDecimalValue, decimalValue);
}
@Test
public void givenValidHexString_whenUsingIntegerDecode_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
int decimalValue = Integer.decode(hexString);
assertEquals(expectedDecimalValue, decimalValue);
}
@Test
public void givenValidHexString_whenUsingBigInteger_thenExpectCorrectDecimalValue() {
String hexString = "0x00FF00";
int expectedDecimalValue = 65280;
BigInteger bigIntegerValue = new BigInteger(hexString.substring(2), 16);
int decimalValue = bigIntegerValue.intValue();
assertEquals(expectedDecimalValue, decimalValue);
}
}