BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program for Decimal to Hexadecimal Conversion

Last Updated: November 2, 2022 by Chaitanya Singh | Filed Under: Java Examples

There are two following ways to convert a 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.

Program 1: Decimal to hexadecimal Using toHexString() method

The toHexString() method accepts integer number as argument and returns equivalent hexadecimal number as a String. The syntax of this method is:

public static String toHexString(int i)

Let’s write a program to convert a given int number to hex number.

class JavaExample
{
  public static void main(String args[])
  {
    //give int value
    int num = 10335;

    // toHexString() converts int to hex
    String str = Integer.toHexString(num);
    System.out.println("Decimal to Hexadecimal: "+str);
  }
}

Output:

Decimal to Hexadecimal: 285f

Get the input number from user: Same program as above, just used Scanner class to get the decimal number input from user.

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

Program 2: Decimal to hexadecimal without using predefined method

In this method, we are not using the predefined method for conversion. We have written a logic to convert the given decimal number to hex.

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

Recommended Posts

  • Java Hexadecimal to Decimal Conversion
  • Java Decimal to Octal Conversion
  • Java Decimal to Binary Conversion
❮ Java Programs

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program for Decimal to Octal Conversion
  3. java program to find factorial of a given number using recursion
  4. Neon Number in Java with example
  5. Sunny Number Program in Java

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap