Skip to content

refactor: HexToOct #5377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 34 additions & 47 deletions src/main/java/com/thealgorithms/conversions/HexToOct.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.conversions;

import java.util.Scanner;

/**
* Converts any Hexadecimal Number to Octal
*
Expand All @@ -12,64 +10,53 @@ private HexToOct() {
}

/**
* This method converts a Hexadecimal number to a decimal number
* Converts a Hexadecimal number to a Decimal number.
*
* @param s The Hexadecimal Number
* @return The Decimal number
* @param hex The Hexadecimal number as a String.
* @return The Decimal equivalent as an integer.
*/
public static int hex2decimal(String s) {
String str = "0123456789ABCDEF";
s = s.toUpperCase();
int val = 0;
for (int i = 0; i < s.length(); i++) {
char a = s.charAt(i);
int n = str.indexOf(a);
val = 16 * val + n;
public static int hexToDecimal(String hex) {
String hexDigits = "0123456789ABCDEF";
hex = hex.toUpperCase();
int decimalValue = 0;

for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
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
* @return The Octal number
* @param decimal The Decimal number as an integer.
* @return The Octal equivalent as an integer.
*/
public static int decimal2octal(int q) {
int now;
int i = 1;
int octnum = 0;
while (q > 0) {
now = q % 8;
octnum = (now * (int) (Math.pow(10, i))) + octnum;
q /= 8;
i++;
public static int decimalToOctal(int decimal) {
int octalValue = 0;
int placeValue = 1;

while (decimal > 0) {
int remainder = decimal % 8;
octalValue += remainder * placeValue;
decimal /= 8;
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) {
String hexadecnum;
int decnum;
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();
public static int hexToOctal(String hex) {
int decimalValue = hexToDecimal(hex);
return decimalToOctal(decimalValue);
}
}
25 changes: 22 additions & 3 deletions src/test/java/com/thealgorithms/conversions/HexToOctTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,29 @@
import org.junit.jupiter.api.Test;

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
public void testDecimalToOctal() {
assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
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 testHexToOct() {
assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48")));
assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD")));
public void testHexToOctal() {
assertEquals(377, HexToOct.hexToOctal("FF"));
assertEquals(20, HexToOct.hexToOctal("10"));
assertEquals(0, HexToOct.hexToOctal("0"));
assertEquals(7777, HexToOct.hexToOctal("FFF"));
}
}