Skip to main content
edited tags
Link
200_success
  • 144.1k
  • 22
  • 188
  • 473
deleted 18 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

(Java) Convert Numbernumber from One Radixone radix to Anotheranother

Tips? CriticismAny tips or criticisms? All help is appreciated.

(Java) Convert Number from One Radix to Another

Tips? Criticism? All help is appreciated.

Convert number from one radix to another

Any tips or criticisms?

added 196 characters in body
Source Link
alecxe
  • 17.3k
  • 8
  • 51
  • 93
class RadixConverter {
 
    // command line arguments: (number) (radix of number) (radix to be converted to)
    public static void main(String[] args) {
        String n = args[0];                                   // n kept as String to support alphabetic representations of numerals for larger radixes
        int startRadix = Integer.parseInt(args[1]);
        int endRadix = Integer.parseInt(args[2]);
        int decimal = convertToDecimal(n, startRadix);
        String result = convertToBase(decimal, endRadix);
        System.out.printf("%s (%d) = %s (%d)%n", n, startRadix, result, endRadix);
    }

    public static int convertToDecimal(String n, int startRadix) {
        int sum = 0;
        for (int i = 0; i < n.length(); i++) {                                                 // begins at first digit and ends at last digit
            char digit = n.charAt(i);
            int placeValue = (int) Math.pow(startRadix, n.length() - i - 1);                   // place value of digit's column
            if ((digit >= 48) && (digit <= 57) && (digit - 48 < startRadix)) {                 // if digit is numerical and is within specified radix
                sum += (digit - 48) * placeValue;
            } else if ((digit >= 65) && (digit <= 90) && (digit - 65 + 10 < startRadix)) {     // if digit is alphabetic and within specified radix
                sum += (digit - 55) * placeValue;
            } else {
                throw new IllegalArgumentException("\n\nThe input number is invalid or contains digits outside of its specified radix.\n");
            }
        }
        return sum;
    }

    public static String convertToBase(int decimal, int endRadix) {
        StringBuilder sb = new StringBuilder("");
        do {
            int rem = decimal % endRadix;
            if ((rem <= 9) && (endRadix <= 36) && (endRadix >= 2)) {
                sb.append(Integer.toString(rem));
            } else if ((rem <= 35) && (endRadix <= 36) && (endRadix >= 2)) {
                sb.append((char) (rem + 55));
            } else {
                throw new IllegalArgumentException("\n\nThe output radix must be from 2 to 36.\n");
            }
            decimal /= endRadix;
        } while (decimal != 0);
        return reverseOrder(sb.toString());
    }

    public static String reverseOrder(String s) {
        StringBuilder sb = new StringBuilder("");
        for (int i = s.length() - 1; i >= 0; i--) {    // executes each character in reverse order
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}

}

class RadixConverter {
 
// command line arguments: (number) (radix of number) (radix to be converted to)
public static void main(String[] args) {
    String n = args[0];                                   // n kept as String to support alphabetic representations of numerals for larger radixes
    int startRadix = Integer.parseInt(args[1]);
    int endRadix = Integer.parseInt(args[2]);
    int decimal = convertToDecimal(n, startRadix);
    String result = convertToBase(decimal, endRadix);
    System.out.printf("%s (%d) = %s (%d)%n", n, startRadix, result, endRadix);
}

public static int convertToDecimal(String n, int startRadix) {
    int sum = 0;
    for (int i = 0; i < n.length(); i++) {                                                 // begins at first digit and ends at last digit
        char digit = n.charAt(i);
        int placeValue = (int) Math.pow(startRadix, n.length() - i - 1);                   // place value of digit's column
        if ((digit >= 48) && (digit <= 57) && (digit - 48 < startRadix)) {                 // if digit is numerical and is within specified radix
            sum += (digit - 48) * placeValue;
        } else if ((digit >= 65) && (digit <= 90) && (digit - 65 + 10 < startRadix)) {     // if digit is alphabetic and within specified radix
            sum += (digit - 55) * placeValue;
        } else {
            throw new IllegalArgumentException("\n\nThe input number is invalid or contains digits outside of its specified radix.\n");
        }
    }
    return sum;
}

public static String convertToBase(int decimal, int endRadix) {
    StringBuilder sb = new StringBuilder("");
    do {
        int rem = decimal % endRadix;
        if ((rem <= 9) && (endRadix <= 36) && (endRadix >= 2)) {
            sb.append(Integer.toString(rem));
        } else if ((rem <= 35) && (endRadix <= 36) && (endRadix >= 2)) {
            sb.append((char) (rem + 55));
        } else {
            throw new IllegalArgumentException("\n\nThe output radix must be from 2 to 36.\n");
        }
        decimal /= endRadix;
    } while (decimal != 0);
    return reverseOrder(sb.toString());
}

public static String reverseOrder(String s) {
    StringBuilder sb = new StringBuilder("");
    for (int i = s.length() - 1; i >= 0; i--) {    // executes each character in reverse order
        sb.append(s.charAt(i));
    }
    return sb.toString();
}

}

class RadixConverter {
    // command line arguments: (number) (radix of number) (radix to be converted to)
    public static void main(String[] args) {
        String n = args[0];                                   // n kept as String to support alphabetic representations of numerals for larger radixes
        int startRadix = Integer.parseInt(args[1]);
        int endRadix = Integer.parseInt(args[2]);
        int decimal = convertToDecimal(n, startRadix);
        String result = convertToBase(decimal, endRadix);
        System.out.printf("%s (%d) = %s (%d)%n", n, startRadix, result, endRadix);
    }

    public static int convertToDecimal(String n, int startRadix) {
        int sum = 0;
        for (int i = 0; i < n.length(); i++) {                                                 // begins at first digit and ends at last digit
            char digit = n.charAt(i);
            int placeValue = (int) Math.pow(startRadix, n.length() - i - 1);                   // place value of digit's column
            if ((digit >= 48) && (digit <= 57) && (digit - 48 < startRadix)) {                 // if digit is numerical and is within specified radix
                sum += (digit - 48) * placeValue;
            } else if ((digit >= 65) && (digit <= 90) && (digit - 65 + 10 < startRadix)) {     // if digit is alphabetic and within specified radix
                sum += (digit - 55) * placeValue;
            } else {
                throw new IllegalArgumentException("\n\nThe input number is invalid or contains digits outside of its specified radix.\n");
            }
        }
        return sum;
    }

    public static String convertToBase(int decimal, int endRadix) {
        StringBuilder sb = new StringBuilder("");
        do {
            int rem = decimal % endRadix;
            if ((rem <= 9) && (endRadix <= 36) && (endRadix >= 2)) {
                sb.append(Integer.toString(rem));
            } else if ((rem <= 35) && (endRadix <= 36) && (endRadix >= 2)) {
                sb.append((char) (rem + 55));
            } else {
                throw new IllegalArgumentException("\n\nThe output radix must be from 2 to 36.\n");
            }
            decimal /= endRadix;
        } while (decimal != 0);
        return reverseOrder(sb.toString());
    }

    public static String reverseOrder(String s) {
        StringBuilder sb = new StringBuilder("");
        for (int i = s.length() - 1; i >= 0; i--) {    // executes each character in reverse order
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}
Source Link
user146374
user146374
Loading