refactor: OctalToHexadecimal (#5345)

This commit is contained in:
Alex Klymenko 2024-08-18 21:03:28 +02:00 committed by GitHub
parent a9f5b82708
commit 33fd79ad55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 47 deletions

View File

@ -1,65 +1,61 @@
package com.thealgorithms.conversions;
import java.util.Scanner;
/**
* Converts any Octal Number to HexaDecimal
* Class for converting an Octal number to its Hexadecimal equivalent.
*
* @author Tanmay Joshi
*/
public final class OctalToHexadecimal {
private static final int OCTAL_BASE = 8;
private static final int HEX_BASE = 16;
private static final String HEX_DIGITS = "0123456789ABCDEF";
private OctalToHexadecimal() {
}
/**
* This method converts a Octal number to a decimal number
* Converts an Octal number (as a string) to its Decimal equivalent.
*
* @param s The Octal Number
* @return The Decimal number
* @param octalNumber The Octal number as a string
* @return The Decimal equivalent of the Octal number
* @throws IllegalArgumentException if the input contains invalid octal digits
*/
public static int octToDec(String s) {
int i = 0;
for (int j = 0; j < s.length(); j++) {
char num = s.charAt(j);
num -= '0';
i *= 8;
i += num;
public static int octalToDecimal(String octalNumber) {
if (octalNumber == null || octalNumber.isEmpty()) {
throw new IllegalArgumentException("Input cannot be null or empty");
}
return i;
int decimalValue = 0;
for (int i = 0; i < octalNumber.length(); i++) {
char currentChar = octalNumber.charAt(i);
if (currentChar < '0' || currentChar > '7') {
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
}
int currentDigit = currentChar - '0';
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
}
return decimalValue;
}
/**
* This method converts a Decimal number to a Hexadecimal number
* Converts a Decimal number to its Hexadecimal equivalent.
*
* @param d The Decimal Number
* @return The Hexadecimal number
* @param decimalNumber The Decimal number
* @return The Hexadecimal equivalent of the Decimal number
*/
public static String decimalToHex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0) {
public static String decimalToHexadecimal(int decimalNumber) {
if (decimalNumber == 0) {
return "0";
}
String hex = "";
while (d > 0) {
int digit = d % 16;
hex = digits.charAt(digit) + hex;
d = d / 16;
}
return hex;
StringBuilder hexValue = new StringBuilder();
while (decimalNumber > 0) {
int digit = decimalNumber % HEX_BASE;
hexValue.insert(0, HEX_DIGITS.charAt(digit));
decimalNumber /= HEX_BASE;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Octal number: ");
// Take octal number as input from user in a string
String oct = input.next();
// Pass the octal number to function and get converted decimal form
int decimal = octToDec(oct);
// Pass the decimal number to function and get converted Hex form of the number
String hex = decimalToHex(decimal);
System.out.println("The Hexadecimal equivalant is: " + hex);
input.close();
return hexValue.toString();
}
}

View File

@ -1,14 +1,23 @@
package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class OctalToHexadecimalTest {
@Test
public void testOctalToHexadecimal() {
assertEquals("1EA", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("752")));
assertEquals("15E", OctalToHexadecimal.decimalToHex(OctalToHexadecimal.octToDec("536")));
@ParameterizedTest
@CsvSource({"0, 0", "7, 7", "10, 8", "17, F", "20, 10", "777, 1FF", "1234, 29C", "752, 1EA", "536, 15E"})
void testCorrectInputs(String inputOctal, String expectedHex) {
int decimal = OctalToHexadecimal.octalToDecimal(inputOctal);
String hex = OctalToHexadecimal.decimalToHexadecimal(decimal);
Assertions.assertEquals(expectedHex, hex);
}
@ParameterizedTest
@CsvSource({"'', Input cannot be null or empty", "'8', Incorrect octal digit: 8", "'19', Incorrect octal digit: 9"})
void testIncorrectInputs(String inputOctal, String expectedMessage) {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> OctalToHexadecimal.octalToDecimal(inputOctal));
Assertions.assertEquals(expectedMessage, exception.getMessage());
}
}