From 1787041bd8f269da448bbbe015bee10d1da2011d Mon Sep 17 00:00:00 2001 From: Alejamdro Date: Thu, 13 Apr 2023 00:01:45 -0300 Subject: [PATCH] BAEL-6078 - Convert an Integer Value into 2-Digit Hex Value in Java (#13682) --- .../core-java-numbers-6/README.md | 2 + core-java-modules/core-java-numbers-6/pom.xml | 18 +++++ .../baeldung/integertohex/IntegerToHex.java | 16 ++++ .../integertohex/IntegerToHexUnitTest.java | 75 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 core-java-modules/core-java-numbers-6/src/main/java/com/baeldung/integertohex/IntegerToHex.java create mode 100644 core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/integertohex/IntegerToHexUnitTest.java diff --git a/core-java-modules/core-java-numbers-6/README.md b/core-java-modules/core-java-numbers-6/README.md index 2c0b0554c4..4856d86052 100644 --- a/core-java-modules/core-java-numbers-6/README.md +++ b/core-java-modules/core-java-numbers-6/README.md @@ -1,2 +1,4 @@ ### Relevant Articles: - [Java Program to Calculate Pi](https://www.baeldung.com/java-monte-carlo-compute-pi) + +- More articles: [[<-- prev]](../core-java-numbers-5) \ No newline at end of file diff --git a/core-java-modules/core-java-numbers-6/pom.xml b/core-java-modules/core-java-numbers-6/pom.xml index 0f78f41b9f..531f1293d1 100644 --- a/core-java-modules/core-java-numbers-6/pom.xml +++ b/core-java-modules/core-java-numbers-6/pom.xml @@ -12,6 +12,21 @@ 0.0.1-SNAPSHOT + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + commons-codec + commons-codec + ${commons-codec} + test + + + core-java-numbers-6 @@ -22,4 +37,7 @@ + + 1.15 + \ No newline at end of file diff --git a/core-java-modules/core-java-numbers-6/src/main/java/com/baeldung/integertohex/IntegerToHex.java b/core-java-modules/core-java-numbers-6/src/main/java/com/baeldung/integertohex/IntegerToHex.java new file mode 100644 index 0000000000..9f75b9a145 --- /dev/null +++ b/core-java-modules/core-java-numbers-6/src/main/java/com/baeldung/integertohex/IntegerToHex.java @@ -0,0 +1,16 @@ +package com.baeldung.integertohex; + +class IntegerToHex { + static final String digits = "0123456789ABCDEF"; + static String integerToHex(int input) { + if (input <= 0) + return "0"; + StringBuilder hex = new StringBuilder(); + while (input > 0) { + int digit = input % 16; + hex.insert(0, digits.charAt(digit)); + input = input / 16; + } + return hex.toString(); + } +} diff --git a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/integertohex/IntegerToHexUnitTest.java b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/integertohex/IntegerToHexUnitTest.java new file mode 100644 index 0000000000..6073c2d347 --- /dev/null +++ b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/integertohex/IntegerToHexUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.integertohex; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.commons.codec.binary.Hex; +import org.junit.jupiter.api.Test; + +class IntegerToHexUnitTest { + + @Test + void givenIntegerValue_whenUseRawMethod_thenWillGetHexValue() { + String result = IntegerToHex.integerToHex(1055); + assertEquals("41F", result); + } + + @Test + void givenIntegerNegativeValue_whenUseRawMethod_thenZeroValue() { + String result = IntegerToHex.integerToHex(-1055); + assertEquals("0", result); + } + + @Test + void givenIntegerPositiveValue_whenUseStringFormat_thenWillGetHexValue() { + String result = String.format("%02x", 255); + assertEquals("ff", result); + } + + @Test + void givenIntegerPositiveValue_whenUseStringFormat_thenWillGetHexValueWithLeftZeros() { + String result = String.format("%04x", 255); + assertEquals("00ff", result); + } + + @Test + void givenIntegerPositiveValue_whenUseStringFormat_thenWillGetHexValueWithLeftZerosAndUpperLetter() { + String result = String.format("%04X", 255); + assertEquals("00FF", result); + } + + @Test + void givenIntegerValue_whenUseIntegerToHexString_thenWillGetHexValue() { + String result = Integer.toHexString(1000); + assertEquals("3e8", result); + } + + @Test + void givenIntegerValue_whenUseLongToHexString_thenWillGetHexValue() { + String result = Long.toHexString(255L); + assertEquals("ff", result); + } + + @Test + public void givenNegativeIntegerValue_whenUseIntegerToString_thenWillGetHexValue() { + String result = Integer.toString(-1458, 16); + assertEquals("-5b2", result); + } + + @Test + public void givenIntegerValue_whenUseIntegerToString_thenWillGetHexValue() { + String result = Integer.toString(1458, 16); + assertEquals("5b2", result); + } + + @Test + public void givenLongValue_whenUseLongToString_thenWillGetHexValue() { + String result = Long.toString(158, 16); + assertEquals("9e", result); + } + + @Test + public void givenIntegerValue_whenUseApacheCommons_thenWillGetHexSignedValue() { + String result = Hex.encodeHexString(new byte[] { (byte) 254 }); + assertEquals("fe", result); + } +} \ No newline at end of file