refactor: BinaryToHexadecimal (#5331)

This commit is contained in:
Alex Klymenko 2024-08-16 16:43:54 +02:00 committed by GitHub
parent ec30592fcb
commit c20375ae0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 39 deletions

View File

@ -1,7 +1,7 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import java.util.HashMap; import java.util.HashMap;
import java.util.Scanner; import java.util.Map;
/** /**
* Converts any Binary Number to a Hexadecimal Number * Converts any Binary Number to a Hexadecimal Number
@ -9,52 +9,55 @@ import java.util.Scanner;
* @author Nishita Aggarwal * @author Nishita Aggarwal
*/ */
public final class BinaryToHexadecimal { public final class BinaryToHexadecimal {
private static final int BITS_IN_HEX_DIGIT = 4;
private static final int BASE_BINARY = 2;
private static final int BASE_DECIMAL = 10;
private static final int HEX_START_DECIMAL = 10;
private static final int HEX_END_DECIMAL = 15;
private BinaryToHexadecimal() { private BinaryToHexadecimal() {
} }
/** /**
* This method converts a binary number to a hexadecimal number. * Converts a binary number to a hexadecimal number.
* *
* @param binary The binary number * @param binary The binary number to convert.
* @return The hexadecimal number * @return The hexadecimal representation of the binary number.
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
*/ */
static String binToHex(int binary) { public static String binToHex(int binary) {
// hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for Map<Integer, String> hexMap = initializeHexMap();
// decimal numbers 0 to 15 StringBuilder hex = new StringBuilder();
HashMap<Integer, String> hm = new HashMap<>();
// String to store hexadecimal code
String hex = "";
int i;
for (i = 0; i < 10; i++) {
hm.put(i, String.valueOf(i));
}
for (i = 10; i < 16; i++) {
hm.put(i, String.valueOf((char) ('A' + i - 10)));
}
int currbit;
while (binary != 0) { while (binary != 0) {
int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits int decimalValue = 0;
for (i = 0; i < 4; i++) { for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
currbit = binary % 10; int currentBit = binary % BASE_DECIMAL;
binary = binary / 10; if (currentBit > 1) {
code4 += currbit * (int) Math.pow(2, i); throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
}
binary /= BASE_DECIMAL;
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
} }
hex = hm.get(code4) + hex; hex.insert(0, hexMap.get(decimalValue));
} }
return hex;
return !hex.isEmpty() ? hex.toString() : "0";
} }
/** /**
* Main method * Initializes the hexadecimal map with decimal to hexadecimal mappings.
* *
* @param args Command line arguments * @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
*/ */
public static void main(String[] args) { private static Map<Integer, String> initializeHexMap() {
Scanner sc = new Scanner(System.in); Map<Integer, String> hexMap = new HashMap<>();
System.out.println("Enter binary number:"); for (int i = 0; i < BASE_DECIMAL; i++) {
int binary = sc.nextInt(); hexMap.put(i, String.valueOf(i));
String hex = binToHex(binary); }
System.out.println("Hexadecimal Code:" + hex); for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) {
sc.close(); hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL)));
}
return hexMap;
} }
} }

View File

@ -1,14 +1,22 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class BinaryToHexadecimalTest { public class BinaryToHexadecimalTest {
@Test @ParameterizedTest
public void testBinaryToHexadecimal() { @CsvSource({"0, 0", "1, 1", "10, 2", "1111, F", "1101010, 6A", "1100, C"})
assertEquals("6A", BinaryToHexadecimal.binToHex(1101010)); void testBinToHex(int binary, String expectedHex) {
assertEquals("C", BinaryToHexadecimal.binToHex(1100)); assertEquals(expectedHex, BinaryToHexadecimal.binToHex(binary));
}
@ParameterizedTest
@CsvSource({"2", "1234", "11112"})
void testInvalidBinaryInput(int binary) {
assertThrows(IllegalArgumentException.class, () -> BinaryToHexadecimal.binToHex(binary));
} }
} }