From 4d7e5eb5c9a143afbe37833cb7a6296fd16b41b6 Mon Sep 17 00:00:00 2001 From: MohamedHelmyKassab <137485958+MohamedHelmyKassab@users.noreply.github.com> Date: Mon, 6 Nov 2023 19:40:42 +0200 Subject: [PATCH] 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. --- .../hextoint/HexToIntConversionUnitTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 core-java-modules/core-java-hex/test/java/com/baeldung/hextoint/HexToIntConversionUnitTest.java diff --git a/core-java-modules/core-java-hex/test/java/com/baeldung/hextoint/HexToIntConversionUnitTest.java b/core-java-modules/core-java-hex/test/java/com/baeldung/hextoint/HexToIntConversionUnitTest.java new file mode 100644 index 0000000000..c236f0a818 --- /dev/null +++ b/core-java-modules/core-java-hex/test/java/com/baeldung/hextoint/HexToIntConversionUnitTest.java @@ -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); + } + + +}