|
1 | 1 | package com.thealgorithms.conversions;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
4 |
| - |
5 | 3 | /**
|
6 |
| - * Converts any Octal Number to HexaDecimal |
| 4 | + * Class for converting an Octal number to its Hexadecimal equivalent. |
7 | 5 | *
|
8 | 6 | * @author Tanmay Joshi
|
9 | 7 | */
|
10 | 8 | public final class OctalToHexadecimal {
|
| 9 | + private static final int OCTAL_BASE = 8; |
| 10 | + private static final int HEX_BASE = 16; |
| 11 | + private static final String HEX_DIGITS = "0123456789ABCDEF"; |
| 12 | + |
11 | 13 | private OctalToHexadecimal() {
|
12 | 14 | }
|
13 | 15 |
|
14 | 16 | /**
|
15 |
| - * This method converts a Octal number to a decimal number |
| 17 | + * Converts an Octal number (as a string) to its Decimal equivalent. |
16 | 18 | *
|
17 |
| - * @param s The Octal Number |
18 |
| - * @return The Decimal number |
| 19 | + * @param octalNumber The Octal number as a string |
| 20 | + * @return The Decimal equivalent of the Octal number |
| 21 | + * @throws IllegalArgumentException if the input contains invalid octal digits |
19 | 22 | */
|
20 |
| - public static int octToDec(String s) { |
21 |
| - int i = 0; |
22 |
| - for (int j = 0; j < s.length(); j++) { |
23 |
| - char num = s.charAt(j); |
24 |
| - num -= '0'; |
25 |
| - i *= 8; |
26 |
| - i += num; |
| 23 | + public static int octalToDecimal(String octalNumber) { |
| 24 | + if (octalNumber == null || octalNumber.isEmpty()) { |
| 25 | + throw new IllegalArgumentException("Input cannot be null or empty"); |
27 | 26 | }
|
28 |
| - return i; |
| 27 | + |
| 28 | + int decimalValue = 0; |
| 29 | + for (int i = 0; i < octalNumber.length(); i++) { |
| 30 | + char currentChar = octalNumber.charAt(i); |
| 31 | + if (currentChar < '0' || currentChar > '7') { |
| 32 | + throw new IllegalArgumentException("Incorrect octal digit: " + currentChar); |
| 33 | + } |
| 34 | + int currentDigit = currentChar - '0'; |
| 35 | + decimalValue = decimalValue * OCTAL_BASE + currentDigit; |
| 36 | + } |
| 37 | + |
| 38 | + return decimalValue; |
29 | 39 | }
|
30 | 40 |
|
31 | 41 | /**
|
32 |
| - * This method converts a Decimal number to a Hexadecimal number |
| 42 | + * Converts a Decimal number to its Hexadecimal equivalent. |
33 | 43 | *
|
34 |
| - * @param d The Decimal Number |
35 |
| - * @return The Hexadecimal number |
| 44 | + * @param decimalNumber The Decimal number |
| 45 | + * @return The Hexadecimal equivalent of the Decimal number |
36 | 46 | */
|
37 |
| - public static String decimalToHex(int d) { |
38 |
| - String digits = "0123456789ABCDEF"; |
39 |
| - if (d <= 0) { |
| 47 | + public static String decimalToHexadecimal(int decimalNumber) { |
| 48 | + if (decimalNumber == 0) { |
40 | 49 | return "0";
|
41 | 50 | }
|
42 |
| - String hex = ""; |
43 |
| - while (d > 0) { |
44 |
| - int digit = d % 16; |
45 |
| - hex = digits.charAt(digit) + hex; |
46 |
| - d = d / 16; |
47 |
| - } |
48 |
| - return hex; |
49 |
| - } |
50 | 51 |
|
51 |
| - public static void main(String[] args) { |
52 |
| - Scanner input = new Scanner(System.in); |
53 |
| - System.out.print("Enter the Octal number: "); |
54 |
| - // Take octal number as input from user in a string |
55 |
| - String oct = input.next(); |
56 |
| - |
57 |
| - // Pass the octal number to function and get converted decimal form |
58 |
| - int decimal = octToDec(oct); |
| 52 | + StringBuilder hexValue = new StringBuilder(); |
| 53 | + while (decimalNumber > 0) { |
| 54 | + int digit = decimalNumber % HEX_BASE; |
| 55 | + hexValue.insert(0, HEX_DIGITS.charAt(digit)); |
| 56 | + decimalNumber /= HEX_BASE; |
| 57 | + } |
59 | 58 |
|
60 |
| - // Pass the decimal number to function and get converted Hex form of the number |
61 |
| - String hex = decimalToHex(decimal); |
62 |
| - System.out.println("The Hexadecimal equivalant is: " + hex); |
63 |
| - input.close(); |
| 59 | + return hexValue.toString(); |
64 | 60 | }
|
65 | 61 | }
|
0 commit comments