Decimal to Hexadecimal Converter in Java

后端 未结 13 2401
有刺的猬
有刺的猬 2020-12-03 13:57

I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a he

13条回答
  •  醉酒成梦
    2020-12-03 14:50

    Check out the code below for decimal to hexadecimal conversion,

    import java.util.Scanner;
    
    public class DecimalToHexadecimal
    {
       public static void main(String[] args)
       {
          int temp, decimalNumber;
          String hexaDecimal = "";
          char hexa[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    
          Scanner sc = new Scanner(System.in);
          System.out.print("Please enter decimal number : ");
          decimalNumber = sc.nextInt();
    
          while(decimalNumber > 0)
          {
             temp = decimalNumber % 16;
             hexaDecimal = hexa[temp] + hexaDecimal;
             decimalNumber = decimalNumber / 16;
          }
    
          System.out.print("The hexadecimal value of " + decimalNumber + " is : " + hexaDecimal);      
          sc.close();
       }
    }
    

    You can learn more on different ways to convert decimal to hexadecimal in the following link >> java convert decimal to hexadecimal.

提交回复
热议问题