Decimal to hexadecimal conversion in java or in any other programming language is overly used activity in any school assignment. Using C or C++. the converting decimal to hexadecimal requires manual division of values to acquire the necessary value of the target base number. However using java, we only require one single call to static method of toHexString of Integer class of java.lang package.

As we have already mentioned in our article Java Hex to Decimal Conversion, Decimal is of base 10 which is our counting number while Hexadecimal is of base 16.

Decimal to Hexadecimal Conversion in Java

The source code provided on the hex to decimal conversion, accepts input from the console. The value inputted should be decimal, invalid value will be catch gracefully.

package com.javatutorialhq.tutorial;

import java.util.InputMismatchException;
import java.util.Scanner;

/*
 * Example java source code to convert Decimal to Hexadecimal
 */

public class DecimalToHexadecimal {

	public static void main(String[] args) {
		System.out.print("Input:");
		// getting the value from the console
		Scanner s = new Scanner(System.in);
		try{
			// using the scanner to get decimal input
			Integer decimalVal = s.nextInt();
			// convert the decimal value to Hex
			String outputHex = Integer.toHexString(decimalVal);
			System.out.println("Hexadecimal Value:"+outputHex);
		}
		catch(InputMismatchException ie){
			//
			System.out.println("Invalid Decimal Input");
		}
		finally{
			// closing the scanner object
			s.close();
		}
	}

}

Sample output in running the JAVA Decimal to Hexadecimal Converter

Input:1234
Hexadecimal Value:4d2