refactor: HexToOct (#5377)

This commit is contained in:
Alex Klymenko 2024-08-24 10:32:50 +02:00 committed by GitHub
parent 44d7cbbaf4
commit aefc8fd4b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 56 additions and 50 deletions

View File

@ -1,7 +1,5 @@
package com.thealgorithms.conversions; package com.thealgorithms.conversions;
import java.util.Scanner;
/** /**
* Converts any Hexadecimal Number to Octal * Converts any Hexadecimal Number to Octal
* *
@ -12,64 +10,53 @@ public final class HexToOct {
} }
/** /**
* This method converts a Hexadecimal number to a decimal number * Converts a Hexadecimal number to a Decimal number.
* *
* @param s The Hexadecimal Number * @param hex The Hexadecimal number as a String.
* @return The Decimal number * @return The Decimal equivalent as an integer.
*/ */
public static int hex2decimal(String s) { public static int hexToDecimal(String hex) {
String str = "0123456789ABCDEF"; String hexDigits = "0123456789ABCDEF";
s = s.toUpperCase(); hex = hex.toUpperCase();
int val = 0; int decimalValue = 0;
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i); for (int i = 0; i < hex.length(); i++) {
int n = str.indexOf(a); char hexChar = hex.charAt(i);
val = 16 * val + n; int digitValue = hexDigits.indexOf(hexChar);
decimalValue = 16 * decimalValue + digitValue;
} }
return val;
return decimalValue;
} }
/** /**
* This method converts a Decimal number to a octal number * Converts a Decimal number to an Octal number.
* *
* @param q The Decimal Number * @param decimal The Decimal number as an integer.
* @return The Octal number * @return The Octal equivalent as an integer.
*/ */
public static int decimal2octal(int q) { public static int decimalToOctal(int decimal) {
int now; int octalValue = 0;
int i = 1; int placeValue = 1;
int octnum = 0;
while (q > 0) { while (decimal > 0) {
now = q % 8; int remainder = decimal % 8;
octnum = (now * (int) (Math.pow(10, i))) + octnum; octalValue += remainder * placeValue;
q /= 8; decimal /= 8;
i++; placeValue *= 10;
} }
octnum /= 10;
return octnum; return octalValue;
} }
/** /**
* Main method that gets the hex input from user and converts it into octal. * Converts a Hexadecimal number to an Octal number.
* *
* @param args arguments * @param hex The Hexadecimal number as a String.
* @return The Octal equivalent as an integer.
*/ */
public static void main(String[] args) { public static int hexToOctal(String hex) {
String hexadecnum; int decimalValue = hexToDecimal(hex);
int decnum; return decimalToOctal(decimalValue);
int octalnum;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Hexadecimal Number : ");
hexadecnum = scan.nextLine();
// first convert hexadecimal to decimal
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
// variable decnum
// convert decimal to octal
octalnum = decimal2octal(decnum);
System.out.println("Number in octal: " + octalnum);
scan.close();
} }
} }

View File

@ -5,10 +5,29 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class HexToOctTest { public class HexToOctTest {
@Test
public void testHexToDecimal() {
assertEquals(255, HexToOct.hexToDecimal("FF"));
assertEquals(16, HexToOct.hexToDecimal("10"));
assertEquals(0, HexToOct.hexToDecimal("0"));
assertEquals(4095, HexToOct.hexToDecimal("FFF"));
}
@Test @Test
public void testHexToOct() { public void testDecimalToOctal() {
assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48"))); assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD"))); assertEquals(255, HexToOct.decimalToOctal(HexToOct.hexToDecimal("AD")));
assertEquals(377, HexToOct.decimalToOctal(255));
assertEquals(20, HexToOct.decimalToOctal(16));
assertEquals(0, HexToOct.decimalToOctal(0));
assertEquals(7777, HexToOct.decimalToOctal(4095));
}
@Test
public void testHexToOctal() {
assertEquals(377, HexToOct.hexToOctal("FF"));
assertEquals(20, HexToOct.hexToOctal("10"));
assertEquals(0, HexToOct.hexToOctal("0"));
assertEquals(7777, HexToOct.hexToOctal("FFF"));
} }
} }