Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dec2Any {
- /*
- * Program: Dec2Any.java
- * Purpose: Convert decimal to any lower base
- * Creator: Chris Clarke
- * Created: 04.04.2012
- */
- public static void main(String[] args) {
- if (args.length == 2) {
- int decimal = Integer.parseInt(args[0]);
- if (decimal < 0) {
- System.out.println("Error: number out of range");
- System.exit(0);
- }
- int base = Integer.parseInt(args[1]);
- if (base < 2 || base > 10) {
- System.out.println("Error: base out of range");
- System.exit(0);
- }
- System.out.println( toBase(decimal, base));
- } else {
- System.out.println("Use: \"java Dec2Any <decimal number> <base>\"");
- }
- }
- static String toBase(int num, int base) {
- String result = "";
- do {
- result = (num % base) + result;
- num = num / base;
- } while (num > 0);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement