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