Skip to content

Commit 4e171c6

Browse files
authored
Merge branch 'master' into refactor/binary_to_decimal
2 parents 6ccca5c + c20375a commit 4e171c6

File tree

4 files changed

+92
-60
lines changed

4 files changed

+92
-60
lines changed
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.thealgorithms.conversions;
22

33
import java.util.HashMap;
4-
import java.util.Scanner;
4+
import java.util.Map;
55

66
/**
77
* Converts any Binary Number to a Hexadecimal Number
88
*
99
* @author Nishita Aggarwal
1010
*/
1111
public final class BinaryToHexadecimal {
12+
private static final int BITS_IN_HEX_DIGIT = 4;
13+
private static final int BASE_BINARY = 2;
14+
private static final int BASE_DECIMAL = 10;
15+
private static final int HEX_START_DECIMAL = 10;
16+
private static final int HEX_END_DECIMAL = 15;
17+
1218
private BinaryToHexadecimal() {
1319
}
1420

1521
/**
16-
* This method converts a binary number to a hexadecimal number.
22+
* Converts a binary number to a hexadecimal number.
1723
*
18-
* @param binary The binary number
19-
* @return The hexadecimal number
24+
* @param binary The binary number to convert.
25+
* @return The hexadecimal representation of the binary number.
26+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
2027
*/
21-
static String binToHex(int binary) {
22-
// hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for
23-
// decimal numbers 0 to 15
24-
HashMap<Integer, String> hm = new HashMap<>();
25-
// String to store hexadecimal code
26-
String hex = "";
27-
int i;
28-
for (i = 0; i < 10; i++) {
29-
hm.put(i, String.valueOf(i));
30-
}
31-
for (i = 10; i < 16; i++) {
32-
hm.put(i, String.valueOf((char) ('A' + i - 10)));
33-
}
34-
int currbit;
28+
public static String binToHex(int binary) {
29+
Map<Integer, String> hexMap = initializeHexMap();
30+
StringBuilder hex = new StringBuilder();
31+
3532
while (binary != 0) {
36-
int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits
37-
for (i = 0; i < 4; i++) {
38-
currbit = binary % 10;
39-
binary = binary / 10;
40-
code4 += currbit * (int) Math.pow(2, i);
33+
int decimalValue = 0;
34+
for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
35+
int currentBit = binary % BASE_DECIMAL;
36+
if (currentBit > 1) {
37+
throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
38+
}
39+
binary /= BASE_DECIMAL;
40+
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
4141
}
42-
hex = hm.get(code4) + hex;
42+
hex.insert(0, hexMap.get(decimalValue));
4343
}
44-
return hex;
44+
45+
return !hex.isEmpty() ? hex.toString() : "0";
4546
}
4647

4748
/**
48-
* Main method
49+
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
4950
*
50-
* @param args Command line arguments
51+
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
5152
*/
52-
public static void main(String[] args) {
53-
Scanner sc = new Scanner(System.in);
54-
System.out.println("Enter binary number:");
55-
int binary = sc.nextInt();
56-
String hex = binToHex(binary);
57-
System.out.println("Hexadecimal Code:" + hex);
58-
sc.close();
53+
private static Map<Integer, String> initializeHexMap() {
54+
Map<Integer, String> hexMap = new HashMap<>();
55+
for (int i = 0; i < BASE_DECIMAL; i++) {
56+
hexMap.put(i, String.valueOf(i));
57+
}
58+
for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) {
59+
hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL)));
60+
}
61+
return hexMap;
5962
}
6063
}
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* This class converts Decimal numbers to Octal Numbers
75
*/
86
public final class DecimalToOctal {
7+
private static final int OCTAL_BASE = 8;
8+
private static final int INITIAL_OCTAL_VALUE = 0;
9+
private static final int INITIAL_PLACE_VALUE = 1;
10+
911
private DecimalToOctal() {
1012
}
1113

1214
/**
13-
* Main Method
15+
* Converts a decimal number to its octal equivalent.
1416
*
15-
* @param args Command line Arguments
17+
* @param decimal The decimal number to convert.
18+
* @return The octal equivalent as an integer.
19+
* @throws IllegalArgumentException if the decimal number is negative.
1620
*/
21+
public static int convertToOctal(int decimal) {
22+
if (decimal < 0) {
23+
throw new IllegalArgumentException("Decimal number cannot be negative.");
24+
}
25+
26+
int octal = INITIAL_OCTAL_VALUE;
27+
int placeValue = INITIAL_PLACE_VALUE;
1728

18-
// enter in a decimal value to get Octal output
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
int n;
22-
int k;
23-
int d;
24-
int s = 0;
25-
int c = 0;
26-
System.out.print("Decimal number: ");
27-
n = sc.nextInt();
28-
k = n;
29-
while (k != 0) {
30-
d = k % 8;
31-
s += d * (int) Math.pow(10, c++);
32-
k /= 8;
29+
while (decimal != 0) {
30+
int remainder = decimal % OCTAL_BASE;
31+
octal += remainder * placeValue;
32+
decimal /= OCTAL_BASE;
33+
placeValue *= 10;
3334
}
3435

35-
System.out.println("Octal equivalent:" + s);
36-
sc.close();
36+
return octal;
3737
}
3838
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
68

79
public class BinaryToHexadecimalTest {
810

9-
@Test
10-
public void testBinaryToHexadecimal() {
11-
assertEquals("6A", BinaryToHexadecimal.binToHex(1101010));
12-
assertEquals("C", BinaryToHexadecimal.binToHex(1100));
11+
@ParameterizedTest
12+
@CsvSource({"0, 0", "1, 1", "10, 2", "1111, F", "1101010, 6A", "1100, C"})
13+
void testBinToHex(int binary, String expectedHex) {
14+
assertEquals(expectedHex, BinaryToHexadecimal.binToHex(binary));
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"2", "1234", "11112"})
19+
void testInvalidBinaryInput(int binary) {
20+
assertThrows(IllegalArgumentException.class, () -> BinaryToHexadecimal.binToHex(binary));
1321
}
1422
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
class DecimalToOctalTest {
11+
@ParameterizedTest
12+
@CsvSource({"0, 0", "7, 7", "8, 10", "10, 12", "64, 100", "83, 123", "7026, 15562"})
13+
void testConvertToOctal(int decimal, int expectedOctal) {
14+
assertEquals(expectedOctal, DecimalToOctal.convertToOctal(decimal));
15+
}
16+
17+
@Test
18+
void testConvertToOctalNegativeNumber() {
19+
assertThrows(IllegalArgumentException.class, () -> DecimalToOctal.convertToOctal(-10));
20+
}
21+
}

0 commit comments

Comments
 (0)