1
1
package com .thealgorithms .conversions ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
/**
6
4
* Converts any Hexadecimal Number to Octal
7
5
*
@@ -12,64 +10,53 @@ private HexToOct() {
12
10
}
13
11
14
12
/**
15
- * This method converts a Hexadecimal number to a decimal number
13
+ * Converts a Hexadecimal number to a Decimal number.
16
14
*
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.
19
17
*/
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 ;
28
27
}
29
- return val ;
28
+
29
+ return decimalValue ;
30
30
}
31
31
32
32
/**
33
- * This method converts a Decimal number to a octal number
33
+ * Converts a Decimal number to an Octal number.
34
34
*
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.
37
37
*/
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 ;
47
47
}
48
- octnum /= 10 ;
49
- return octnum ;
48
+
49
+ return octalValue ;
50
50
}
51
51
52
52
/**
53
- * Main method that gets the hex input from user and converts it into octal .
53
+ * Converts a Hexadecimal number to an Octal number .
54
54
*
55
- * @param args arguments
55
+ * @param hex The Hexadecimal number as a String.
56
+ * @return The Octal equivalent as an integer.
56
57
*/
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 );
74
61
}
75
62
}
0 commit comments