BAEL-6078 - Convert an Integer Value into 2-Digit Hex Value in Java (#13682)

This commit is contained in:
Alejamdro 2023-04-13 00:01:45 -03:00 committed by GitHub
parent 725deaa8d5
commit 1787041bd8
4 changed files with 111 additions and 0 deletions

View File

@ -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)

View File

@ -12,6 +12,21 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-numbers-6</finalName>
<resources>
@ -22,4 +37,7 @@
</resources>
</build>
<properties>
<commons-codec>1.15</commons-codec>
</properties>
</project>

View File

@ -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();
}
}

View File

@ -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);
}
}