Answers for "java from number to hex"

1

Java convert decimal to hex

import java.util.Scanner;
public class DecimalToHexaExample 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter decimal number: ");
      int decimal = sc.nextInt();
      String strHexadecimal = "";
      while(decimal != 0)
      {
         int hexNumber = decimal % 16;
         char charHex;
         if(hexNumber <= 9 && hexNumber >= 0)
         {
            charHex = (char)(hexNumber + '0');
         }
         else
         {
            charHex = (char)(hexNumber - 10 + 'A');
         }
         strHexadecimal = charHex + strHexadecimal;
         decimal = decimal / 16;
      }
      System.out.println("Hexadecimal number: " + strHexadecimal);
      sc.close();
   }
}
Posted by: Guest on January-14-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language