How To Convert Decimal Number To Hexadecimal Number In A Java Program

Introduction :-

Introduction :-

Hello friends, Today let us begin with a new Java program. Many of serious programmers who are just beginning to code in Java, come across a problem that is, How to convert Decimals to Hexadecimals in a Java program. Learning Java by an example is the most effective technique to inculcate this versatile language. Generally, whatever you know about language its additional when you have power to code small snippets. Learning by coding will not only give you command over the language but also develop the skills in you to code successfully.Hexadecimals are often used in computer systems


package com.hubberspot.conversion.example;

import java.util.Scanner;

public class DecimalToHexConversion {

public static void main(String[] args) {

Scanner input = new Scanner(;

System.out.print("Enter decimal number you like: ");
int deci = input.nextInt();

System.out.println("The hexadecimal number for decimal " +
deci + " is " + convert(deci));

public static String convert(int decimal) {
String hex = "";

while (decimal != 0) {
int hexValue = decimal % 16;
hex = toHexChar(hexValue)

+ hex;
decimal = decimal / 16;

return hex;

public static char toHexChar(int hexValue) {
if (hexValue <= 9 && hexValue >= 0)
return (char)(hexValue + '0');
return (char)(hexValue - 10 + 'A');

Output of the program :

Enter a decimal number: 10
The hex number for decimal 10 is A


For more of such hot topics read my other posts here :

Visit my new website

Article Written By jontymagicman

I am DInesh Varyani.

Last updated on 26-07-2016 240 0

Please login to comment on this post.
There are no comments yet.
How Do I Sort Array Elements Using Bubble Sort Algorithm ?
While, Do-while, For And For-each Loop Statements In Java