Skip to content

Commit 6fabf24

Browse files
author
alxkm
committed
refactor: HexToOct
1 parent ce4eb55 commit 6fabf24

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed
Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Converts any Hexadecimal Number to Octal
75
*
@@ -12,64 +10,53 @@ private HexToOct() {
1210
}
1311

1412
/**
15-
* This method converts a Hexadecimal number to a decimal number
13+
* Converts a Hexadecimal number to a Decimal number.
1614
*
17-
* @param s The Hexadecimal Number
18-
* @return The Decimal number
15+
* @param hex The Hexadecimal number as a String.
16+
* @return The Decimal equivalent as an integer.
1917
*/
20-
public static int hex2decimal(String s) {
21-
String str = "0123456789ABCDEF";
22-
s = s.toUpperCase();
23-
int val = 0;
24-
for (int i = 0; i < s.length(); i++) {
25-
char a = s.charAt(i);
26-
int n = str.indexOf(a);
27-
val = 16 * val + n;
18+
public static int hexToDecimal(String hex) {
19+
String hexDigits = "0123456789ABCDEF";
20+
hex = hex.toUpperCase();
21+
int decimalValue = 0;
22+
23+
for (int i = 0; i < hex.length(); i++) {
24+
char hexChar = hex.charAt(i);
25+
int digitValue = hexDigits.indexOf(hexChar);
26+
decimalValue = 16 * decimalValue + digitValue;
2827
}
29-
return val;
28+
29+
return decimalValue;
3030
}
3131

3232
/**
33-
* This method converts a Decimal number to a octal number
33+
* Converts a Decimal number to an Octal number.
3434
*
35-
* @param q The Decimal Number
36-
* @return The Octal number
35+
* @param decimal The Decimal number as an integer.
36+
* @return The Octal equivalent as an integer.
3737
*/
38-
public static int decimal2octal(int q) {
39-
int now;
40-
int i = 1;
41-
int octnum = 0;
42-
while (q > 0) {
43-
now = q % 8;
44-
octnum = (now * (int) (Math.pow(10, i))) + octnum;
45-
q /= 8;
46-
i++;
38+
public static int decimalToOctal(int decimal) {
39+
int octalValue = 0;
40+
int placeValue = 1;
41+
42+
while (decimal > 0) {
43+
int remainder = decimal % 8;
44+
octalValue += remainder * placeValue;
45+
decimal /= 8;
46+
placeValue *= 10;
4747
}
48-
octnum /= 10;
49-
return octnum;
48+
49+
return octalValue;
5050
}
5151

5252
/**
53-
* Main method that gets the hex input from user and converts it into octal.
53+
* Converts a Hexadecimal number to an Octal number.
5454
*
55-
* @param args arguments
55+
* @param hex The Hexadecimal number as a String.
56+
* @return The Octal equivalent as an integer.
5657
*/
57-
public static void main(String[] args) {
58-
String hexadecnum;
59-
int decnum;
60-
int octalnum;
61-
Scanner scan = new Scanner(System.in);
62-
63-
System.out.print("Enter Hexadecimal Number : ");
64-
hexadecnum = scan.nextLine();
65-
66-
// first convert hexadecimal to decimal
67-
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
68-
// variable decnum
69-
70-
// convert decimal to octal
71-
octalnum = decimal2octal(decnum);
72-
System.out.println("Number in octal: " + octalnum);
73-
scan.close();
58+
public static int hexToOctal(String hex) {
59+
int decimalValue = hexToDecimal(hex);
60+
return decimalToOctal(decimalValue);
7461
}
7562
}

src/test/java/com/thealgorithms/conversions/HexToOctTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
import org.junit.jupiter.api.Test;
66

77
public class HexToOctTest {
8+
@Test
9+
public void testHexToDecimal() {
10+
assertEquals(255, HexToOct.hexToDecimal("FF"));
11+
assertEquals(16, HexToOct.hexToDecimal("10"));
12+
assertEquals(0, HexToOct.hexToDecimal("0"));
13+
assertEquals(4095, HexToOct.hexToDecimal("FFF"));
14+
}
15+
16+
@Test
17+
public void testDecimalToOctal() {
18+
assertEquals(110, HexToOct.decimalToOctal(HexToOct.hexToDecimal("48")));
19+
assertEquals(255, HexToOct.decimalToOctal(HexToOct.hexToDecimal("AD")));
20+
assertEquals(377, HexToOct.decimalToOctal(255));
21+
assertEquals(20, HexToOct.decimalToOctal(16));
22+
assertEquals(0, HexToOct.decimalToOctal(0));
23+
assertEquals(7777, HexToOct.decimalToOctal(4095));
24+
}
825

926
@Test
10-
public void testHexToOct() {
11-
assertEquals(110, HexToOct.decimal2octal(HexToOct.hex2decimal("48")));
12-
assertEquals(255, HexToOct.decimal2octal(HexToOct.hex2decimal("AD")));
27+
public void testHexToOctal() {
28+
assertEquals(377, HexToOct.hexToOctal("FF"));
29+
assertEquals(20, HexToOct.hexToOctal("10"));
30+
assertEquals(0, HexToOct.hexToOctal("0"));
31+
assertEquals(7777, HexToOct.hexToOctal("FFF"));
1332
}
1433
}

0 commit comments

Comments
 (0)