beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java program to convert decimal to hexadecimal

By Chaitanya Singh | Filed Under: Java Examples

There are two following ways to convert Decimal number to hexadecimal number:

1) Using toHexString() method of Integer class.
2) Do conversion by writing your own logic without using any predefined methods.

Method 1: Decimal to hexadecimal Using toHexString() method

import java.util.Scanner;
class DecimalToHexExample
{
    public static void main(String args[])
    {
      Scanner input = new Scanner( System.in );
      System.out.print("Enter a decimal number : ");
      int num =input.nextInt();
        
      // calling method toHexString()
      String str = Integer.toHexString(num);
      System.out.println("Method 1: Decimal to hexadecimal: "+str);
    }
}

Output:

Enter a decimal number : 123
Method 1: Decimal to hexadecimal: 7b

Method 2: Decimal to hexadecimal without using predefined method

import java.util.Scanner;
class DecimalToHexExample
{
   public static void main(String args[])
   {
     Scanner input = new Scanner( System.in );
     System.out.print("Enter a decimal number : ");
     int num =input.nextInt();
        
     // For storing remainder
     int rem;
        
     // For storing result
     String str2=""; 
 
     // Digits in hexadecimal number system
     char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
 
     while(num>0)
     {
       rem=num%16; 
       str2=hex[rem]+str2; 
       num=num/16;
     }
     System.out.println("Method 2: Decimal to hexadecimal: "+str2);
  }
}

Output:

Enter a decimal number : 123
Method 2: Decimal to hexadecimal: 7B

Enjoyed this post? Try these related posts

  1. Java Program to print Odd numbers from 1 to n or 1 to 100
  2. Java Program to Calculate average using Array
  3. Java Program to Make a Calculator using Switch Case
  4. Java Program to Add Two Matrix using Multi-dimensional Arrays
  5. Java program to display prime numbers from 1 to 100 and 1 to n
  6. Java Program to find largest of three Numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap