refactor: DecimalToHexadecimal
(#5337)
This commit is contained in:
parent
d80fd0c623
commit
25b6aebe45
@ -1,51 +0,0 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
// hex = [0 - 9] -> [A - F]
|
||||
final class DecimalToHexaDecimal {
|
||||
private DecimalToHexaDecimal() {
|
||||
}
|
||||
|
||||
private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
|
||||
private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
|
||||
private static final int HALF_BYTE = 0x0F;
|
||||
private static final char[] HEX_DIGITS = {
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'A',
|
||||
'B',
|
||||
'C',
|
||||
'D',
|
||||
'E',
|
||||
'F',
|
||||
};
|
||||
|
||||
// Returns the hex value of the dec entered in the parameter.
|
||||
public static String decToHex(int dec) {
|
||||
StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
|
||||
hexBuilder.setLength(SIZE_OF_INT_IN_HALF_BYTES);
|
||||
for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
|
||||
int j = dec & HALF_BYTE;
|
||||
hexBuilder.setCharAt(i, HEX_DIGITS[j]);
|
||||
dec >>= NUMBER_OF_BITS_IN_HALF_BYTE;
|
||||
}
|
||||
return hexBuilder.toString().toLowerCase();
|
||||
}
|
||||
|
||||
// Test above function.
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Test...");
|
||||
int dec = 305445566;
|
||||
String libraryDecToHex = Integer.toHexString(dec);
|
||||
String decToHex = decToHex(dec);
|
||||
System.out.println("Result from the library : " + libraryDecToHex);
|
||||
System.out.println("Result decToHex method : " + decToHex);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
/**
|
||||
* This class provides a method to convert a decimal number to a hexadecimal string.
|
||||
*/
|
||||
final class DecimalToHexadecimal {
|
||||
private static final int SIZE_OF_INT_IN_HALF_BYTES = 8;
|
||||
private static final int NUMBER_OF_BITS_IN_HALF_BYTE = 4;
|
||||
private static final int HALF_BYTE_MASK = 0x0F;
|
||||
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
|
||||
private DecimalToHexadecimal() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a decimal number to a hexadecimal string.
|
||||
* @param decimal the decimal number to convert
|
||||
* @return the hexadecimal representation of the decimal number
|
||||
*/
|
||||
public static String decToHex(int decimal) {
|
||||
StringBuilder hexBuilder = new StringBuilder(SIZE_OF_INT_IN_HALF_BYTES);
|
||||
for (int i = SIZE_OF_INT_IN_HALF_BYTES - 1; i >= 0; --i) {
|
||||
int currentHalfByte = decimal & HALF_BYTE_MASK;
|
||||
hexBuilder.insert(0, HEX_DIGITS[currentHalfByte]);
|
||||
decimal >>= NUMBER_OF_BITS_IN_HALF_BYTE;
|
||||
}
|
||||
return removeLeadingZeros(hexBuilder.toString().toLowerCase());
|
||||
}
|
||||
|
||||
private static String removeLeadingZeros(String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < str.length() && str.charAt(i) == '0') {
|
||||
i++;
|
||||
}
|
||||
|
||||
return i == str.length() ? "0" : str.substring(i);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DecimalToHexaDecimalTest {
|
||||
|
||||
@Test
|
||||
public void testDecimalToHexaDecimal() {
|
||||
assertEquals("000000be", DecimalToHexaDecimal.decToHex(190));
|
||||
assertEquals("00000708", DecimalToHexaDecimal.decToHex(1800));
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
public class DecimalToHexadecimalTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({"0, 0", "1, 1", "10, a", "15, f", "16, 10", "255, ff", "190, be", "1800, 708"})
|
||||
void testDecToHex(int decimal, String expectedHex) {
|
||||
assertEquals(expectedHex, DecimalToHexadecimal.decToHex(decimal));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user