| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a23bac9 commit 5d59a2e
219 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,130 +6,122 @@ | |||
| 6 | 6 | import java.util.Scanner; | |
| 7 | 7 | ||
| 8 | 8 | /** | |
| 9 | - * Class for converting from "any" base to "any" other base, when "any" means from 2-36. | ||
| 10 | - * Works by going from base 1 to decimal to base 2. Includes auxiliary method for | ||
| 11 | - * determining whether a number is valid for a given base. | ||
| 9 | + * Class for converting from "any" base to "any" other base, when "any" means from 2-36. Works by | ||
| 10 | + * going from base 1 to decimal to base 2. Includes auxiliary method for determining whether a | ||
| 11 | + * number is valid for a given base. | ||
| 12 | 12 | * | |
| 13 | 13 | * @author Michael Rolland | |
| 14 | 14 | * @version 2017.10.10 | |
| 15 | 15 | */ | |
| 16 | 16 | public class AnyBaseToAnyBase { | |
| 17 | 17 | ||
| 18 | - /** | ||
| 19 | - * Smallest and largest base you want to accept as valid input | ||
| 20 | - */ | ||
| 21 | - static final int MINIMUM_BASE = 2; | ||
| 22 | - static final int MAXIMUM_BASE = 36; | ||
| 18 | + /** Smallest and largest base you want to accept as valid input */ | ||
| 19 | + static final int MINIMUM_BASE = 2; | ||
| 23 | 20 | ||
| 24 | - public static void main(String[] args) { | ||
| 25 | - Scanner in = new Scanner(System.in); | ||
| 26 | - String n; | ||
| 27 | - int b1, b2; | ||
| 28 | - while (true) { | ||
| 29 | - try { | ||
| 30 | - System.out.print("Enter number: "); | ||
| 31 | - n = in.next(); | ||
| 32 | - System.out.print("Enter beginning base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); | ||
| 33 | - b1 = in.nextInt(); | ||
| 34 | - if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) { | ||
| 35 | - System.out.println("Invalid base!"); | ||
| 36 | - continue; | ||
| 37 | - } | ||
| 38 | - if (!validForBase(n, b1)) { | ||
| 39 | - System.out.println("The number is invalid for this base!"); | ||
| 40 | - continue; | ||
| 41 | - } | ||
| 42 | - System.out.print("Enter end base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); | ||
| 43 | - b2 = in.nextInt(); | ||
| 44 | - if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) { | ||
| 45 | - System.out.println("Invalid base!"); | ||
| 46 | - continue; | ||
| 47 | - } | ||
| 48 | - break; | ||
| 49 | - } catch (InputMismatchException e) { | ||
| 50 | - System.out.println("Invalid input."); | ||
| 51 | - in.next(); | ||
| 52 | - } | ||
| 21 | + static final int MAXIMUM_BASE = 36; | ||
| 22 | + | ||
| 23 | + public static void main(String[] args) { | ||
| 24 | + Scanner in = new Scanner(System.in); | ||
| 25 | + String n; | ||
| 26 | + int b1, b2; | ||
| 27 | + while (true) { | ||
| 28 | + try { | ||
| 29 | + System.out.print("Enter number: "); | ||
| 30 | + n = in.next(); | ||
| 31 | + System.out.print( | ||
| 32 | + "Enter beginning base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); | ||
| 33 | + b1 = in.nextInt(); | ||
| 34 | + if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) { | ||
| 35 | + System.out.println("Invalid base!"); | ||
| 36 | + continue; | ||
| 37 | + } | ||
| 38 | + if (!validForBase(n, b1)) { | ||
| 39 | + System.out.println("The number is invalid for this base!"); | ||
| 40 | + continue; | ||
| 53 | 41 | } | |
| 54 | - System.out.println(base2base(n, b1, b2)); | ||
| 55 | - in.close(); | ||
| 42 | + System.out.print( | ||
| 43 | + "Enter end base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): "); | ||
| 44 | + b2 = in.nextInt(); | ||
| 45 | + if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) { | ||
| 46 | + System.out.println("Invalid base!"); | ||
| 47 | + continue; | ||
| 48 | + } | ||
| 49 | + break; | ||
| 50 | + } catch (InputMismatchException e) { | ||
| 51 | + System.out.println("Invalid input."); | ||
| 52 | + in.next(); | ||
| 53 | + } | ||
| 56 | 54 | } | |
| 55 | + System.out.println(base2base(n, b1, b2)); | ||
| 56 | + in.close(); | ||
| 57 | + } | ||
| 57 | 58 | ||
| 58 | - /** | ||
| 59 | - * Checks if a number (as a String) is valid for a given base. | ||
| 60 | - */ | ||
| 61 | - public static boolean validForBase(String n, int base) { | ||
| 62 | - char[] validDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', | ||
| 63 | - 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | ||
| 64 | - 'W', 'X', 'Y', 'Z'}; | ||
| 65 | - // digitsForBase contains all the valid digits for the base given | ||
| 66 | - char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base); | ||
| 59 | + /** Checks if a number (as a String) is valid for a given base. */ | ||
| 60 | + public static boolean validForBase(String n, int base) { | ||
| 61 | + char[] validDigits = { | ||
| 62 | + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', | ||
| 63 | + 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' | ||
| 64 | + }; | ||
| 65 | + // digitsForBase contains all the valid digits for the base given | ||
| 66 | + char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base); | ||
| 67 | 67 | ||
| 68 | - // Convert character array into set for convenience of contains() method | ||
| 69 | - HashSet<Character> digitsList = new HashSet<>(); | ||
| 70 | - for (int i = 0; i < digitsForBase.length; i++) | ||
| 71 | - digitsList.add(digitsForBase[i]); | ||
| 68 | + // Convert character array into set for convenience of contains() method | ||
| 69 | + HashSet<Character> digitsList = new HashSet<>(); | ||
| 70 | + for (int i = 0; i < digitsForBase.length; i++) digitsList.add(digitsForBase[i]); | ||
| 72 | 71 | ||
| 73 | - // Check that every digit in n is within the list of valid digits for that base. | ||
| 74 | - for (char c : n.toCharArray()) | ||
| 75 | - if (!digitsList.contains(c)) | ||
| 76 | - return false; | ||
| 72 | + // Check that every digit in n is within the list of valid digits for that base. | ||
| 73 | + for (char c : n.toCharArray()) if (!digitsList.contains(c)) return false; | ||
| 77 | 74 | ||
| 78 | - return true; | ||
| 79 | - } | ||
| 75 | + return true; | ||
| 76 | + } | ||
| 80 | 77 | ||
| 81 | - /** | ||
| 82 | - * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal, | ||
| 83 | - * then decimal to b2. | ||
| 84 | - * | ||
| 85 | - * @param n The integer to be converted. | ||
| 86 | - * @param b1 Beginning base. | ||
| 87 | - * @param b2 End base. | ||
| 88 | - * @return n in base b2. | ||
| 89 | - */ | ||
| 90 | - public static String base2base(String n, int b1, int b2) { | ||
| 91 | - // Declare variables: decimal value of n, | ||
| 92 | - // character of base b1, character of base b2, | ||
| 93 | - // and the string that will be returned. | ||
| 94 | - int decimalValue = 0, charB2; | ||
| 95 | - char charB1; | ||
| 96 | - String output = ""; | ||
| 97 | - // Go through every character of n | ||
| 98 | - for (int i = 0; i < n.length(); i++) { | ||
| 99 | - // store the character in charB1 | ||
| 100 | - charB1 = n.charAt(i); | ||
| 101 | - // if it is a non-number, convert it to a decimal value >9 and store it in charB2 | ||
| 102 | - if (charB1 >= 'A' && charB1 <= 'Z') | ||
| 103 | - charB2 = 10 + (charB1 - 'A'); | ||
| 104 | - // Else, store the integer value in charB2 | ||
| 105 | - else | ||
| 106 | - charB2 = charB1 - '0'; | ||
| 107 | - // Convert the digit to decimal and add it to the | ||
| 108 | - // decimalValue of n | ||
| 109 | - decimalValue = decimalValue * b1 + charB2; | ||
| 110 | - } | ||
| 78 | + /** | ||
| 79 | + * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal, | ||
| 80 | + * then decimal to b2. | ||
| 81 | + * | ||
| 82 | + * @param n The integer to be converted. | ||
| 83 | + * @param b1 Beginning base. | ||
| 84 | + * @param b2 End base. | ||
| 85 | + * @return n in base b2. | ||
| 86 | + */ | ||
| 87 | + public static String base2base(String n, int b1, int b2) { | ||
| 88 | + // Declare variables: decimal value of n, | ||
| 89 | + // character of base b1, character of base b2, | ||
| 90 | + // and the string that will be returned. | ||
| 91 | + int decimalValue = 0, charB2; | ||
| 92 | + char charB1; | ||
| 93 | + String output = ""; | ||
| 94 | + // Go through every character of n | ||
| 95 | + for (int i = 0; i < n.length(); i++) { | ||
| 96 | + // store the character in charB1 | ||
| 97 | + charB1 = n.charAt(i); | ||
| 98 | + // if it is a non-number, convert it to a decimal value >9 and store it in charB2 | ||
| 99 | + if (charB1 >= 'A' && charB1 <= 'Z') charB2 = 10 + (charB1 - 'A'); | ||
| 100 | + // Else, store the integer value in charB2 | ||
| 101 | + else charB2 = charB1 - '0'; | ||
| 102 | + // Convert the digit to decimal and add it to the | ||
| 103 | + // decimalValue of n | ||
| 104 | + decimalValue = decimalValue * b1 + charB2; | ||
| 105 | + } | ||
| 111 | 106 | ||
| 112 | - // Converting the decimal value to base b2: | ||
| 113 | - // A number is converted from decimal to another base | ||
| 114 | - // by continuously dividing by the base and recording | ||
| 115 | - // the remainder until the quotient is zero. The number in the | ||
| 116 | - // new base is the remainders, with the last remainder | ||
| 117 | - // being the left-most digit. | ||
| 118 | - if (0 == decimalValue) | ||
| 119 | - return "0"; | ||
| 120 | - // While the quotient is NOT zero: | ||
| 121 | - while (decimalValue != 0) { | ||
| 122 | - // If the remainder is a digit < 10, simply add it to | ||
| 123 | - // the left side of the new number. | ||
| 124 | - if (decimalValue % b2 < 10) | ||
| 125 | - output = Integer.toString(decimalValue % b2) + output; | ||
| 126 | - // If the remainder is >= 10, add a character with the | ||
| 127 | - // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) | ||
| 128 | - else | ||
| 129 | - output = (char) ((decimalValue % b2) + 55) + output; | ||
| 130 | - // Divide by the new base again | ||
| 131 | - decimalValue /= b2; | ||
| 132 | - } | ||
| 133 | - return output; | ||
| 107 | + // Converting the decimal value to base b2: | ||
| 108 | + // A number is converted from decimal to another base | ||
| 109 | + // by continuously dividing by the base and recording | ||
| 110 | + // the remainder until the quotient is zero. The number in the | ||
| 111 | + // new base is the remainders, with the last remainder | ||
| 112 | + // being the left-most digit. | ||
| 113 | + if (0 == decimalValue) return "0"; | ||
| 114 | + // While the quotient is NOT zero: | ||
| 115 | + while (decimalValue != 0) { | ||
| 116 | + // If the remainder is a digit < 10, simply add it to | ||
| 117 | + // the left side of the new number. | ||
| 118 | + if (decimalValue % b2 < 10) output = Integer.toString(decimalValue % b2) + output; | ||
| 119 | + // If the remainder is >= 10, add a character with the | ||
| 120 | + // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) | ||
| 121 | + else output = (char) ((decimalValue % b2) + 55) + output; | ||
| 122 | + // Divide by the new base again | ||
| 123 | + decimalValue /= b2; | ||
| 134 | 124 | } | |
| 125 | + return output; | ||
| 126 | + } | ||
| 135 | 127 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,53 +1,51 @@ | |||
| 1 | 1 | package Conversions; | |
| 2 | 2 | ||
| 3 | - /** | ||
| 4 | - * @author Varun Upadhyay (https://github.com/varunu28) | ||
| 5 | - */ | ||
| 3 | + /** @author Varun Upadhyay (https://github.com/varunu28) */ | ||
| 6 | 4 | ||
| 7 | 5 | // Driver program | |
| 8 | 6 | public class AnyBaseToDecimal { | |
| 9 | - public static void main(String[] args) { | ||
| 10 | - assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2); | ||
| 11 | - assert convertToDecimal("777", 8) == Integer.valueOf("777", 8); | ||
| 12 | - assert convertToDecimal("999", 10) == Integer.valueOf("999", 10); | ||
| 13 | - assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16); | ||
| 14 | - assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36); | ||
| 15 | - } | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2); | ||
| 9 | + assert convertToDecimal("777", 8) == Integer.valueOf("777", 8); | ||
| 10 | + assert convertToDecimal("999", 10) == Integer.valueOf("999", 10); | ||
| 11 | + assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16); | ||
| 12 | + assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36); | ||
| 13 | + } | ||
| 16 | 14 | ||
| 17 | - /** | ||
| 18 | - * Convert any radix to decimal number | ||
| 19 | - * | ||
| 20 | - * @param s the string to be convert | ||
| 21 | - * @param radix the radix | ||
| 22 | - * @return decimal of bits | ||
| 23 | - * @throws NumberFormatException if {@code bits} or {@code radix} is invalid | ||
| 24 | - */ | ||
| 25 | - public static int convertToDecimal(String s, int radix) { | ||
| 26 | - int num = 0; | ||
| 27 | - int pow = 1; | ||
| 15 | + /** | ||
| 16 | + * Convert any radix to decimal number | ||
| 17 | + * | ||
| 18 | + * @param s the string to be convert | ||
| 19 | + * @param radix the radix | ||
| 20 | + * @return decimal of bits | ||
| 21 | + * @throws NumberFormatException if {@code bits} or {@code radix} is invalid | ||
| 22 | + */ | ||
| 23 | + public static int convertToDecimal(String s, int radix) { | ||
| 24 | + int num = 0; | ||
| 25 | + int pow = 1; | ||
| 28 | 26 | ||
| 29 | - for (int i = s.length() - 1; i >= 0; i--) { | ||
| 30 | - int digit = valOfChar(s.charAt(i)); | ||
| 31 | - if (digit >= radix) { | ||
| 32 | - throw new NumberFormatException("For input string " + s); | ||
| 33 | - } | ||
| 34 | - num += valOfChar(s.charAt(i)) * pow; | ||
| 35 | - pow *= radix; | ||
| 36 | - } | ||
| 37 | - return num; | ||
| 27 | + for (int i = s.length() - 1; i >= 0; i--) { | ||
| 28 | + int digit = valOfChar(s.charAt(i)); | ||
| 29 | + if (digit >= radix) { | ||
| 30 | + throw new NumberFormatException("For input string " + s); | ||
| 31 | + } | ||
| 32 | + num += valOfChar(s.charAt(i)) * pow; | ||
| 33 | + pow *= radix; | ||
| 38 | 34 | } | |
| 35 | + return num; | ||
| 36 | + } | ||
| 39 | 37 | ||
| 40 | - /** | ||
| 41 | - * Convert character to integer | ||
| 42 | - * | ||
| 43 | - * @param c the character | ||
| 44 | - * @return represented digit of given character | ||
| 45 | - * @throws NumberFormatException if {@code ch} is not UpperCase or Digit character. | ||
| 46 | - */ | ||
| 47 | - public static int valOfChar(char c) { | ||
| 48 | - if (!(Character.isUpperCase(c) || Character.isDigit(c))) { | ||
| 49 | - throw new NumberFormatException("invalid character :" + c); | ||
| 50 | - } | ||
| 51 | - return Character.isDigit(c) ? c - '0' : c - 'A' + 10; | ||
| 38 | + /** | ||
| 39 | + * Convert character to integer | ||
| 40 | + * | ||
| 41 | + * @param c the character | ||
| 42 | + * @return represented digit of given character | ||
| 43 | + * @throws NumberFormatException if {@code ch} is not UpperCase or Digit character. | ||
| 44 | + */ | ||
| 45 | + public static int valOfChar(char c) { | ||
| 46 | + if (!(Character.isUpperCase(c) || Character.isDigit(c))) { | ||
| 47 | + throw new NumberFormatException("invalid character :" + c); | ||
| 52 | 48 | } | |
| 49 | + return Character.isDigit(c) ? c - '0' : c - 'A' + 10; | ||
| 50 | + } | ||
| 53 | 51 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,30 +1,30 @@ | |||
| 1 | 1 | package Conversions; | |
| 2 | 2 | ||
| 3 | 3 | import java.util.Scanner; | |
| 4 | - //given a source number , source base, destination base, this code can give you the destination number. | ||
| 5 | - //sn ,sb,db ---> ()dn . this is what we have to do . | ||
| 4 | + // given a source number , source base, destination base, this code can give you the destination | ||
| 5 | + // number. | ||
| 6 | + // sn ,sb,db ---> ()dn . this is what we have to do . | ||
| 6 | 7 | ||
| 7 | 8 | public class AnytoAny { | |
| 8 | 9 | ||
| 9 | - public static void main(String[] args) { | ||
| 10 | - Scanner scn = new Scanner(System.in); | ||
| 11 | - int sn = scn.nextInt(); | ||
| 12 | - int sb = scn.nextInt(); | ||
| 13 | - int db = scn.nextInt(); | ||
| 14 | - int m = 1, dec = 0, dn = 0; | ||
| 15 | - while (sn != 0) { | ||
| 16 | - dec = dec + (sn % 10) * m; | ||
| 17 | - m *= sb; | ||
| 18 | - sn /= 10; | ||
| 19 | - } | ||
| 20 | - m = 1; | ||
| 21 | - while (dec != 0) { | ||
| 22 | - dn = dn + (dec % db) * m; | ||
| 23 | - m *= 10; | ||
| 24 | - dec /= db; | ||
| 25 | - } | ||
| 26 | - System.out.println(dn); | ||
| 27 | - scn.close(); | ||
| 10 | + public static void main(String[] args) { | ||
| 11 | + Scanner scn = new Scanner(System.in); | ||
| 12 | + int sn = scn.nextInt(); | ||
| 13 | + int sb = scn.nextInt(); | ||
| 14 | + int db = scn.nextInt(); | ||
| 15 | + int m = 1, dec = 0, dn = 0; | ||
| 16 | + while (sn != 0) { | ||
| 17 | + dec = dec + (sn % 10) * m; | ||
| 18 | + m *= sb; | ||
| 19 | + sn /= 10; | ||
| 28 | 20 | } | |
| 29 | - | ||
| 21 | + m = 1; | ||
| 22 | + while (dec != 0) { | ||
| 23 | + dn = dn + (dec % db) * m; | ||
| 24 | + m *= 10; | ||
| 25 | + dec /= db; | ||
| 26 | + } | ||
| 27 | + System.out.println(dn); | ||
| 28 | + scn.close(); | ||
| 29 | + } | ||
| 30 | 30 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments