×

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers to their appropriate binary, octal, decimal, hexadecimal, or other base numbers. To do the following conversion in Java, we can utilize existing packages or user-defined methods.

Steps to find Duodecimal Number:

To find the duodecimal number of any given number, perform the following steps:

  • As input, take a hexadecimal number from the user.
  • Create a custom method to convert it to decimal.
  • We also write a user-defined function that converts decimal numbers to duodecimal numbers.
  • Return the duodecimal number.

DuoDecimalDemoExample.java

import java.util.*;  
class DuoDecimalDemoExample {  
// To discover the equivalent duodecimal number of a given hexadecimal number , define the class DuodecimalDemoExample. 
    public static String convToDeci (String val , int bas , Map<Character , Integer> hexToDec)  
// construct a convToDeci () method that returns the decimal equivalent of the given hexadecimal value
    {  
        int add = 0;  
        int pow = 0;  
        String tem_Data = val;  
          // finding corresponding decimal number 
       
        for (int i = tem_Data.length () - 1; i >= 0; i--) {  
            int value = tem_Data.charAt (i) - '0';  
            if (bas == 16 && hexToDec.containsKey (tem_Data.charAt (i))) {  
                value = hexToDec.get (tem_Data.charAt (i));  
            }  
            add += (value) * (Math.pow (bas, pow++));  
        }   
        return String.valueOf (add);  
    }  
  
    public static String convertToDuoDecimal (String val , int bas , Map<Integer , Character>dectoHex , Map<Character , Integer>hextoDec)  
// develop a convertToDuoDecimal () method that converts a decimal number to a Duodecimal value and returns it to the main () method
    {  
        String val1 = val;  
        int nBase = bas;  
  // verify base is decimal or not 
         
        if (nBase != 10) {  
            // If the base is not 10, it calls the convToDeci () function, which returns the appropriate decimal number of the provided number.
            val = convToDeci (val, bas, hextoDec);  
              
             //  We update the value of newBase to 10 after converting the number.
            nBase = 10;  
        }  
          
        
        int tempo = Integer.parseInt (val1);  
        int rem;    
        String duoDecimal = "";     
          
      
        char duoChars [] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'};    
           
        while (tempo > 0)    
         {    
           rem = tempo % 12;     
           duoDecimal = duoChars [rem]+duoDecimal;     
           tempo = tempo / 12;    
         }    
         return duoDecimal;  
    }  
      
    public static void main (String [] args)  
    {  
      
        String val1;  
          
        
        Scanner sc0 = new Scanner (System.in);  
        System.out.print ("enter hexadecimal number: ");  
        val1 = sc0.nextLine ();  
          
        
        sc0.close ();  
          


        Map <Character , Integer> hexToDec = new HashMap<> ();  
        Map <Integer , Character> dectoHex = new HashMap<> ();  
          
       
        for (int i = 0; i < 6; i++) {  
            dectoHex.put (10 + i , (char) ('A' + i));  
            hexToDec.put ((char) ('A' + i) ,  10 + i);  
        }  
          
          
        System.out.println ("Duodecimal : " + convertToDuoDecimal (val1 , 16 , dectoHex , hexToDec));  
          
          
    }  
}  

Output:

Duodecimal in Java

Other than hexadecimal, we must construct user-defined methods such as the convertToDec () function, which will convert the supplied integer into decimal and return it for calculating the duodecimal number.


Related Topics

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Java Polymorphism

The process of representing one form in multiple forms is known as Polymorphism. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means...

5 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.