Converter.java


Below is the syntax highlighted version of Converter.java from §6.1 Data Representations.


/******************************************************************************
 *  Compilation:  javac Converter.java
 *  Execution:    java Converter s baseFrom baseTo
 *
 *  Converts a positive integer to and from its base b representation.
 *  No error checking is currently implemented.
 *
 *  Remarks
 *  -------
 *   - See also Integer.toHexString(), Integer.toBinaryString()
 *     and Integer.toString()
 *   - See also Integer.parseInt()
 *
 *  % java Converter
 *  1101101 2 10
 *  109
 *
 *  FACE 16 10
 *  64206
 *
 *  FACE 16 2
 *  1111101011001110
 *
 *  109 10 2
 *  1101101
 *
 *  64206 10 16
 *  FACE
 *
 *  64206 10 32
 *  1UME
 *
 *  1UME 32 10
 *  64206
 *
 *  0 10 2
 *  0
 *
 ******************************************************************************/

public class Converter {

   // converts integer n into a base b string
    public static String toString(int n, int base) {
        // special case
        if (n == 0) return "0";

        String s = "";
        while (n > 0) {
            s = toChar(n % base) + s;
            n = n / base;
        }
        return s;
    }

    // converts integer n into a base b string
    public static String toStringRecursive(int n, int base) {
        if (n == 0) return "";
        return toStringRecursive(n/base, base) + toChar(n % base);
    }

    // convert a String representing a base b integer into an int
    public static int parseInt(String s, int base) {
        int n = 0;
        for (int i = 0; i < s.length(); i++)
            n = base*n + toInt(s.charAt(i));
        return n;
    }

    public static int toInt(char c) {
        if (c < '0' || c > 'Z') throw new IllegalArgumentException("invalid char");
        if ((c >= '0') && (c <= '9')) return c - '0';
        return c - 'A' + 10;
    }

    public static char toChar(int i) {
        if (i < 0 || i > 36) throw new IllegalArgumentException("invalid digit");
        if (i < 10) return (char) ('0' + i);
        return (char) ('A' + i - 10);
    }


    // sample test client
    public static void main(String[] args) {
        while (!StdIn.isEmpty()) {
            String s = StdIn.readString();
            int baseFrom = StdIn.readInt();
            int baseTo = StdIn.readInt();
            int n = parseInt(s, baseFrom);
            StdOut.println(toString(n, baseTo));
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:32:25 EDT 2022.