Skip to content

Commit de0eccc

Browse files
author
alxkm
committed
fix: revert
1 parent b69066f commit de0eccc

File tree

2 files changed

+48
-55
lines changed

2 files changed

+48
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
11
package com.thealgorithms.conversions;
22

3+
import java.util.Scanner;
4+
35
/**
4-
* Class for converting an Octal number to its Hexadecimal equivalent.
6+
* Converts any Octal Number to HexaDecimal
57
*
8+
* @author Tanmay Joshi
69
*/
710
public final class OctalToHexadecimal {
8-
private static final int OCTAL_BASE = 8;
9-
private static final int HEX_BASE = 16;
10-
private static final String HEX_DIGITS = "0123456789ABCDEF";
11-
1211
private OctalToHexadecimal() {
1312
}
1413

1514
/**
16-
* Converts an Octal number (as a string) to its Decimal equivalent.
15+
* This method converts a Octal number to a decimal number
1716
*
18-
* @param octalNumber The Octal number as a string
19-
* @return The Decimal equivalent of the Octal number
20-
* @throws IllegalArgumentException if the input contains invalid octal digits
17+
* @param s The Octal Number
18+
* @return The Decimal number
2119
*/
22-
public static int octalToDecimal(String octalNumber) {
23-
if (octalNumber == null || octalNumber.isEmpty()) {
24-
throw new IllegalArgumentException("Input cannot be null or empty");
20+
public static int octToDec(String s) {
21+
int i = 0;
22+
for (int j = 0; j < s.length(); j++) {
23+
char num = s.charAt(j);
24+
num -= '0';
25+
i *= 8;
26+
i += num;
2527
}
26-
27-
int decimalValue = 0;
28-
for (int i = 0; i < octalNumber.length(); i++) {
29-
char currentChar = octalNumber.charAt(i);
30-
if (currentChar < '0' || currentChar > '7') {
31-
throw new IllegalArgumentException("Invalid octal digit: " + currentChar);
32-
}
33-
int currentDigit = currentChar - '0';
34-
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
35-
}
36-
37-
return decimalValue;
28+
return i;
3829
}
3930

4031
/**
41-
* Converts a Decimal number to its Hexadecimal equivalent.
32+
* This method converts a Decimal number to a Hexadecimal number
4233
*
43-
* @param decimalNumber The Decimal number
44-
* @return The Hexadecimal equivalent of the Decimal number
34+
* @param d The Decimal Number
35+
* @return The Hexadecimal number
4536
*/
46-
public static String decimalToHexadecimal(int decimalNumber) {
47-
if (decimalNumber == 0) {
37+
public static String decimalToHex(int d) {
38+
String digits = "0123456789ABCDEF";
39+
if (d <= 0) {
4840
return "0";
4941
}
50-
51-
StringBuilder hexValue = new StringBuilder();
52-
while (decimalNumber > 0) {
53-
int digit = decimalNumber % HEX_BASE;
54-
hexValue.insert(0, HEX_DIGITS.charAt(digit));
55-
decimalNumber /= HEX_BASE;
42+
String hex = "";
43+
while (d > 0) {
44+
int digit = d % 16;
45+
hex = digits.charAt(digit) + hex;
46+
d = d / 16;
5647
}
48+
return hex;
49+
}
50+
51+
public static void main(String[] args) {
52+
Scanner input = new Scanner(System.in);
53+
System.out.print("Enter the Octal number: ");
54+
// Take octal number as input from user in a string
55+
String oct = input.next();
56+
57+
// Pass the octal number to function and get converted decimal form
58+
int decimal = octToDec(oct);
5759

58-
return hexValue.toString();
60+
// Pass the decimal number to function and get converted Hex form of the number
61+
String hex = decimalToHex(decimal);
62+
System.out.println("The Hexadecimal equivalant is: " + hex);
63+
input.close();
5964
}
6065
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
package com.thealgorithms.conversions;
22

3-
import org.junit.jupiter.api.Assertions;
4-
import org.junit.jupiter.params.ParameterizedTest;
5-
import org.junit.jupiter.params.provider.CsvSource;
3+
import static org.junit.jupiter.api.Assertions.*;
64

7-
public class OctalToHexadecimalTest {
5+
import org.junit.jupiter.api.Test;
86

9-
@ParameterizedTest
10-
@CsvSource({"0, 0", "7, 7", "10, 8", "17, F", "20, 10", "777, 1FF", "1234, 29C", "752, 1EA", "536, 15E"})
11-
void testOctalToHexadecimal_ValidInputs(String inputOctal, String expectedHex) {
12-
int decimal = OctalToHexadecimal.octalToDecimal(inputOctal);
13-
String hex = OctalToHexadecimal.decimalToHexadecimal(decimal);
14-
Assertions.assertEquals(expectedHex, hex);
15-
}
7+
public class OctalToHexadecimalTest {
168

17-
@ParameterizedTest
18-
@CsvSource({"'', Input cannot be null or empty", "'8', Invalid octal digit: 8", "'19', Invalid octal digit: 9"})
19-
void testOctalToHexadecimal_InvalidInputs(String inputOctal, String expectedMessage) {
20-
IllegalArgumentException exception = Assertions.assertThrows(
21-
IllegalArgumentException.class,
22-
() -> OctalToHexadecimal.octalToDecimal(inputOctal)
23-
);
24-
Assertions.assertEquals(expectedMessage, exception.getMessage());
9+
@Test
10+
public void testOctalToHexadecimal() {
11+
assertEquals("1EA", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("752")));
12+
assertEquals("15E", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("536")));
2513
}
2614
}

0 commit comments

Comments
 (0)