|
1 | 1 | package com.thealgorithms.conversions;
|
2 | 2 |
|
3 |
| -import java.io.BufferedReader; |
4 |
| -import java.io.InputStreamReader; |
5 | 3 | import java.util.ArrayList;
|
| 4 | +import java.util.List; |
6 | 5 |
|
7 | 6 | /**
|
| 7 | + * Class that provides methods to convert a decimal number to a string representation |
| 8 | + * in any specified base between 2 and 36. |
| 9 | + * |
8 | 10 | * @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
|
9 | 11 | */
|
10 |
| -// Driver Program |
11 | 12 | public final class DecimalToAnyBase {
|
12 |
| - private DecimalToAnyBase() { |
13 |
| - } |
14 |
| - |
15 |
| - public static void main(String[] args) throws Exception { |
16 |
| - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
17 |
| - System.out.println("Enter the decimal input below: "); |
18 |
| - int decInput = Integer.parseInt(br.readLine()); |
19 |
| - System.out.println(); |
20 |
| - |
21 |
| - System.out.println("Enter the base below: "); |
22 |
| - int base = Integer.parseInt(br.readLine()); |
23 |
| - System.out.println(); |
| 13 | + private static final int MIN_BASE = 2; |
| 14 | + private static final int MAX_BASE = 36; |
| 15 | + private static final char ZERO_CHAR = '0'; |
| 16 | + private static final char A_CHAR = 'A'; |
| 17 | + private static final int DIGIT_OFFSET = 10; |
24 | 18 |
|
25 |
| - System.out.println("Decimal Input" |
26 |
| - + " is: " + decInput); |
27 |
| - System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base)); |
28 |
| - |
29 |
| - br.close(); |
| 19 | + private DecimalToAnyBase() { |
30 | 20 | }
|
31 | 21 |
|
32 | 22 | /**
|
33 |
| - * This method produces a String value of any given input decimal in any |
34 |
| - * base |
| 23 | + * Converts a decimal number to a string representation in the specified base. |
| 24 | + * For example, converting the decimal number 10 to base 2 would return "1010". |
35 | 25 | *
|
36 |
| - * @param inp Decimal of which we need the value in base in String format |
37 |
| - * @return string format of the converted value in the given base |
| 26 | + * @param decimal the decimal number to convert |
| 27 | + * @param base the base to convert to (must be between {@value #MIN_BASE} and {@value #MAX_BASE}) |
| 28 | + * @return the string representation of the number in the specified base |
| 29 | + * @throws IllegalArgumentException if the base is out of the supported range |
38 | 30 | */
|
39 |
| - public static String convertToAnyBase(int inp, int base) { |
40 |
| - ArrayList<Character> charArr = new ArrayList<>(); |
| 31 | + public static String convertToAnyBase(int decimal, int base) { |
| 32 | + if (base < MIN_BASE || base > MAX_BASE) { |
| 33 | + throw new IllegalArgumentException("Base must be between " + MIN_BASE + " and " + MAX_BASE); |
| 34 | + } |
41 | 35 |
|
42 |
| - while (inp > 0) { |
43 |
| - charArr.add(reVal(inp % base)); |
44 |
| - inp /= base; |
| 36 | + if (decimal == 0) { |
| 37 | + return String.valueOf(ZERO_CHAR); |
45 | 38 | }
|
46 | 39 |
|
47 |
| - StringBuilder str = new StringBuilder(charArr.size()); |
| 40 | + List<Character> digits = new ArrayList<>(); |
| 41 | + while (decimal > 0) { |
| 42 | + digits.add(convertToChar(decimal % base)); |
| 43 | + decimal /= base; |
| 44 | + } |
48 | 45 |
|
49 |
| - for (Character ch : charArr) { |
50 |
| - str.append(ch); |
| 46 | + StringBuilder result = new StringBuilder(digits.size()); |
| 47 | + for (int i = digits.size() - 1; i >= 0; i--) { |
| 48 | + result.append(digits.get(i)); |
51 | 49 | }
|
52 | 50 |
|
53 |
| - return str.reverse().toString(); |
| 51 | + return result.toString(); |
54 | 52 | }
|
55 | 53 |
|
56 | 54 | /**
|
57 |
| - * This method produces character value of the input integer and returns it |
| 55 | + * Converts an integer value to its corresponding character in the specified base. |
| 56 | + * This method is used to convert values from 0 to 35 into their appropriate character representation. |
| 57 | + * For example, 0-9 are represented as '0'-'9', and 10-35 are represented as 'A'-'Z'. |
58 | 58 | *
|
59 |
| - * @param num integer of which we need the character value of |
60 |
| - * @return character value of input integer |
| 59 | + * @param value the integer value to convert (should be less than the base value) |
| 60 | + * @return the character representing the value in the specified base |
61 | 61 | */
|
62 |
| - public static char reVal(int num) { |
63 |
| - if (num >= 0 && num <= 9) { |
64 |
| - return (char) (num + '0'); |
| 62 | + private static char convertToChar(int value) { |
| 63 | + if (value >= 0 && value <= 9) { |
| 64 | + return (char) (ZERO_CHAR + value); |
65 | 65 | } else {
|
66 |
| - return (char) (num - 10 + 'A'); |
| 66 | + return (char) (A_CHAR + value - DIGIT_OFFSET); |
67 | 67 | }
|
68 | 68 | }
|
69 | 69 | }
|
0 commit comments