Open In App

DuoDecimal in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Duodecimal represents a system which is a notation system in which a number having its base as 12 is called a duodecimal number. In java, we can use to find the conversation of duodecimal numbers into respective binary, octal, decimal, hexadecimal number, or any other base numbers. In java, we can use predefined packages or user-defined methods to perform the following conversion.

Let us portray out few sample sets  of duodecimal conversation into other based numbers which are listed below as follows:

The duodecimal number of (15b)12 is converted to the binary number (11010111)2

The duodecimal number of (182)12 is converted to the octal number (362)8

The duodecimal number of (262)12 is converted to the decimal number (362)10

The duodecimal number of(288052)12 is converted to the hexadecimal number (A563E)16

The duodecimal number of (58576a2)12 is converted to the hexatrigesimal number (A563E)32

Procedure: 

There are certain steps required to convert into a duodecimal number which is listed below as follows:

  1. Take a hexadecimal number as user input.
  2. Create a user-defined function to convert it into a decimal number.
  3. Create another user-defined function that will convert a decimal number into a duodecimal number.
  4. Print the resultant duodecimal number.

Example:

Java




// Java Program Illustrating DuodecimalNumber via Conversion
// of Hexadecimal Numbers into Duodecimal Numbers
 
// Importing utility classes
import java.util.*;
 
// Main class
// Representing equivalent duodecimal No of Hexadecimal No
class Main {
 
    // Method 1
    // Returning the decimal number of the given hexadecimal
    // number
    public static String
    convertToDec(String value, int base,
                 Map<Character, Integer> hexatoDec)
    {
 
        int sum = 0;
        int pow = 0;
        String tempData = value;
 
        // Logic to find equivalent decimal number
        for (int i = tempData.length() - 1; i >= 0; i--) {
 
            // charAt() represents element at 'i'th index
            int val = tempData.charAt(i) - '0';
 
            if (base == 16
                && hexatoDec.containsKey(
                    tempData.charAt(i))) {
                val = hexatoDec.get(tempData.charAt(i));
            }
 
            // Math.pow() calculates x^n
            sum += (val) * (Math.pow(base, pow++));
        }
 
        return String.valueOf(sum);
    }
 
    // Method 2
    // Converting decimal number into Duodecimal number and
    // return it into main() method.
    public static String
    convertToDuoDecimal(String value, int base,
                        Map<Integer, Character> dectoHex,
                        Map<Character, Integer> hextoDec)
    {
 
        String val = value;
        int newBase = base;
 
        // Checks whether the base is decimal or not
        if (newBase != 10) {
 
            // If the base is not 10, it call the
            // convertToDec() method which return the
            // corresponding decimal number of the given
            // number.
            val = convertToDec(value, base, hextoDec);
 
            // After converting the number, new base is
            // updated Say be it 10
            newBase = 10;
        }
 
        // Converting the string number into integer
        // using parseInt()
        int temp = Integer.parseInt(val);
        int rem;
        String duoDecimal = "";
 
        // Creating duoDecimalChars[] array for defining the
        // characters
        char duoDecimalChars[]
            = { '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', 'A', 'B' };
 
        // Logic to find equivalent duodecimal number
        while (temp > 0) {
 
            rem = temp % 12;
            duoDecimal = duoDecimalChars[rem] + duoDecimal;
            temp = temp / 12;
        }
        return duoDecimal;
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a variable to store hexadecimal number
        String val;
 
        // Custom input in main() for hexadecimal number
        val = "3A4C2";
 
        // Creating a hexatoDec and dectoHexa for storing
        // values by creating object of Map class Declaring
        // object of character and integer type
        Map<Character, Integer> hexatoDec = new HashMap<>();
        Map<Integer, Character> dectoHex = new HashMap<>();
 
        // Logic to store date into hexatoDec and dectoHexa
        // map
        for (int i = 0; i < 6; i++) {
            dectoHex.put(10 + i, (char)('A' + i));
            hexatoDec.put((char)('A' + i), 10 + i);
        }
 
        // Call the convertToDuoDecimal() and printing the
        // returned value of it.
        System.out.println(
            "Duodecimal : "
            + convertToDuoDecimal(val, 16, dectoHex,
                                  hexatoDec));
    }
}


Output

Duodecimal : B622A

 



Last Updated : 26 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads