Pin It

How to convert Decimals to Hexadecimals in a Java program ?

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 programming.

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 : 

Video tutorial to demonstrate how to convert Decimal number to Hexadecimal number in Java.

Please Give Us Your 1 Minute In Sharing This Post!
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
© 2012 Learn Java by Examples Template by Hubber Spot