Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static String UNKNOWN = "";

    /**
     * Convert decimalism to other.
     * @param decimal Value in decimalism.
     * @param radix Which system to convert. (Only 2, 8, 16)
     */
    public static String toBits(int decimal, int radix) {
        String num;
        switch (radix) {
        case 2:
            num = "00000000000000000000000000000000" + Integer.toBinaryString(decimal);
            return "0b" + num.substring(num.length() - 32);
        case 8:
            num = "00000000000" + Integer.toOctalString(decimal);
            return "0" + num.substring(num.length() - 11);
        case 16:
            num = "00000000" + Integer.toHexString(decimal);
            return "0x" + num.substring(num.length() - 8);
        default:
            return UNKNOWN;
        }
    }
}