20+ Java Hexadecimal to Decimal Converters

This post contains a total of 20+ Hand-Picked Java Hexadecimal to Decimal Converter examples with Source Code. All the Hexadecimal to decimal converters are made using Java programming language.

You can use the source codes of these programs for your own personal or educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Matheus Rambo

Made by Matheus Rambo. Simple Java program to convert hexadecimal to decimal. ( Source )

``````import java.util.Scanner;

public static void main(String[] args) {

ConvertTo convert = new ConvertTo();

Scanner in = new Scanner(System.in);

String hexa1 = in.next(); // let the user input in lowercase

String hexa = hexa1.toUpperCase(); // converts to uppercase

if (VerifyHexaNumber.isHexa(hexa) == true) {
System.out.println("Hexa: "+ hexa);
System.out.print("Decimal: ");
System.out.println(convert.returnValue(hexa));

} else {
}

System.out.println("        Thanks\n    Matheus Rambo!");
}
}

/**
* Verify if the number is hexadecimal
* @author matheus
*/
class VerifyHexaNumber {

public static boolean isHexa(String hexa) {
char hexaChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int hexaLength = hexa.length();
int count = 0;

for (byte i = 0; i < hexaLength; i++) {
char actual = hexa.charAt(i);

for (byte arrayHexa = 0; arrayHexa < hexaChars.length; arrayHexa++) {
if (actual == hexaChars[arrayHexa]) {
++count; // if actual is equal at some element, count receive more one!
}
}

}

return hexaLength == count;

}

}

class ConvertTo {

/**
* Returns the equivalent number!
* @param number
* @return
*/

private int switchElement(String number) {

switch (number) {
case "A":
return 10;
case "B":
return 11;
case "C":
return 12;
case "D":
return 13;
case "E":
return 14;
case "F":
return 15;
default:
return 0;
}

}

public long returnValue(String number) {

long valorRetornar = 0;
int value = 0;
String valor = "";
int vezes = 0;

for (int i = number.length() - 1; i >= 0; i--) {

valor = String.valueOf(number.charAt(i));

try {

value = Integer.parseInt(valor);

} catch (NumberFormatException ex) {

value = switchElement(valor);

} finally {

int sixTeen = 16;

if (vezes > 1) {

for (int z = vezes; z > 1; z--) {
sixTeen *= 16;
}

value = value * sixTeen;

} else if (vezes == 1) {

value = value * 16;
}

++vezes;

valorRetornar += value;
}

}
return valorRetornar;
}
}``````

2. By AU Hameed

Made by AU Hameed. ( Source )

``````/**
Program Purpose: Converts Hexadecimal Number to a Decimal Number
**/

import java.util.Scanner;

public class HexToDecConversion
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);

//prompt the user to enter a string
String hex = input.nextLine();

System.out.println("The decimal value for hexadecimal number " + hex + " is " + hexToDecimal(hex.toUpperCase()));
}

public static int hexToDecimal(String hex)
{
int decimalValue = 0;
for(int i = 0; i < hex.length(); i++)
{
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
}
return decimalValue;
}

public static int hexCharToDecimal(char ch)
{
if(ch >= 'A' && ch <= 'F')
return 10 + ch - 'A';
else // ch is '0', '1' , ..., or '9'
return ch - '0';
}
}
``````

3. By Chris

Made by Chris. ( Source )

``````import java.util.Scanner;

public class Program
{
/** Main method */
public static void main(String[] args) {
// Create a scanner
Scanner input = new Scanner(System.in);

//Prompt the user to enter a string
String hex = input.nextLine();

System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal(hex.toUpperCase()));
}
/** Hex to decimal */
public static int hexToDecimal(String hex){
int decimalValue = 0;
for (int i = 0; i < hex.length(); i++){
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
}
return decimalValue;
}

/** hex character to decimal */
public static int hexCharToDecimal(char ch){
if (ch >= 'A' && ch <= 'F')
return 10 + ch - 'A';
else
return ch - '0';
}
}``````

4. By ÁÑÀÑYÁÀBHÎK

Made by ÁÑÀÑYÁÀBHÎK. ( Source )

``````import java.util.Scanner;
{
public static void main(String args[])
{
String digits="0123456789ABCDEF";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number of Hexadecimal Number system");
String str=sc.nextLine();
int x=str.length();
int d,f=727;
int val=0;
for(int i=0;i<x;i++)
{
char c=str.charAt(i);
d=digits.indexOf(c);
if(d!=-1)
{
val=(val*16)+d;
}
else
{
f=1;
break;
}
}
if(f!=1)
System.out.println("Decimal value="+val);
else
System.out.println("Wrong Input");
}
}``````

5. By Lord Teacrumble

Made by Lord Teacrumble. To get a decimal value from hexadecimal inpu,t enter hex, then the hex value. ( Source )

``````import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.Math;
import java.util.Collections;
import java.util.HashMap;
public class Program
{
public static void main(String[] args){
/*
////////////////////////////////////////
ENTER hex, bin OR dec AS STARTVALUE
ENTER ACTUAL VALUE
GET RESULTS
////////////////////////////////////////
*/
try{
System.out.println("Enter the type of number you want to convert (hex/bin/dec)");
switch(choice){
case "hex":
checkHexa(hexa);
System.out.println("Hexadecimal notation : " + hexa);
System.out.println("Decimal notation : " + hexDec(hexa));
System.out.println("Binary notation : " + hexBin(hexa));
break;
case "dec":
System.out.println("Enter a decimal number");
System.out.println("Decimal notation : " + decimal);
System.out.println("Binary notation : " + decBin(decimal));
System.out.println("Hexadecimal notation : " + decHex(decimal));
break;
case "bin":
System.out.println("Enter a binary");
System.out.println("Binary notation : " + binary);
System.out.println("Decimal notation : " + binDec(binary));
System.out.println("Hexadecimal notation : " + binHex(binary));
break;
default:
System.out.println("choose one of the 3 choices \n(hex/bin/dec) " + choice);
}
}
catch(Exception ex){
System.out.println("woopsie " + ex.getMessage());
}
}

/*
*/
public static String decHex(int number){
if(number < 16){
return hexNum(number);
}
else{
return decHex(number/16) + hexNum(number%16);
}
}

/*
Converts decimal numbers to hexadecimal numbers
*/
public static String hexNum(int num){
String result;
switch(num){
case 10:
result = "A";
break;
case 11:
result = "B";
break;
case 12:
result = "C";
break;
case 13:
result = "D";
break;
case 14:
result = "E";
break;
case 15:
result = "F";
break;
default:
result = "" + num;
break;
}
return result;
}

/*
Converts decimals to binary
*/
private static String decBin(int number){
if(number%2 == 1){
if(number == 1){
return "1";
}
else{
return decBin(number/2) + "1";
}
}
else{
return decBin(number/2) + "0";
}
}

/*
check if input = binary
*/

private static void checkBin(int[] bin) throws Exception {

for(int t: bin){
if(t>1){
throw new Exception("enter a binary number");
}
}
}

/*
Binary to decimal
*/

private static String binDec(int binary) throws Exception {
try{
String sBinar = Integer.toString(binary);
int[] split = new int[sBinar.length()];
for(int c= sBinar.length()-1; c>= 0; c--){
split[c] = sBinar.charAt(c)-'0';
}
checkBin(split);
int multi = 1;
int result = 0;
for(int i = sBinar.length()-1; i>=0; i--){
result += split[i]*multi;
multi*= 2;
}

return Integer.toString(result);
}
catch (Exception ex){
throw new Exception(ex.getMessage());
}
}

/*
*/

private static String binHex(int binary) throws Exception {
String sBinar = Integer.toString(binary);
int[] split = new int[sBinar.length()];
for(int c= sBinar.length()-1; c>= 0; c--){
split[c] = sBinar.charAt(c)-'0';
}
int multi = 1;
int counter = 0;
int result = 0;
List <String> results = new ArrayList <String>();
for(int i = sBinar.length()-1; i>=0; i--){
counter++;
result += split[i]*multi;
multi*=2;
if(counter == 4|| i==0){
multi = 1;
counter = 0;
result = 0;
}
}
Collections.reverse(results);
String total = "";
for(Iterator <String> i = results.iterator(); i.hasNext(); ){
String s = i.next();
total+= s;
}
}

/*

*/

private static void checkHexa(String hex) throws Exception{
char[] arrHex;
arrHex = hex.toCharArray();
for(char c: arrHex){
if(!((c>= 48 && c<=57)||(c>=65 && c<=90))){
throw new Exception("this is not a valid hexadecimal number");
}
}
}

/*
*/

private static int toDec(char hexn){
String returnNumber;
switch(hexn){
case 'A':
returnNumber = "10";
break;
case 'B':
returnNumber = "11";
break;
case 'C':
returnNumber = "12";
break;
case 'D':
returnNumber = "13";
break;
case 'E':
returnNumber = "14";
break;
case 'F':
returnNumber = "15";
break;
default:
returnNumber="" + hexn;
}
return Integer.parseInt(returnNumber);
}

/*
*/
private static String hexDec(String hex){
char[] arrHex;
arrHex = hex.toCharArray();
int result = 0;
List<Integer> arrDec = new ArrayList <Integer>();
for(int e = 0; e<arrHex.length; e++){
}
Collections.reverse(arrDec);
int counter = 0;
for(Iterator <Integer> i = arrDec.iterator(); i.hasNext(); ){
int num = i.next();
result += num*(Math.pow(16, counter));
counter++;
}
return Integer.toString(result);
}

/*
*/

char[] arrayH;
HashMap<Character,String> dict = new HashMap<Character,String>();
String result = "";
dict.put('1',"0001");
dict.put('2',"0010");
dict.put('3',"0011");
dict.put('4',"0100");
dict.put('5',"0101");
dict.put('6', "0110");
dict.put('7', "0111");
dict.put('8', "1000");
dict.put('9', "1001");
dict.put('A', "1010");
dict.put('B', "1011");
dict.put('C', "1100");
dict.put('D', "1101");
dict.put('E', "1110");
dict.put('F', "1111");
for(char c: arrayH){
result += dict.get(c);
}
return result;
}

}``````

6. By Malek Alsset

Made by Malek Alsset. Change the hexadecimal number in line ‘String hexnum = “6a”;’. ( Source )

``````public class MA{
public static void main(String args[]){
String hexnum = "6a";

//converting hex to decimal by passing base 16
int num = Integer.parseInt(hexnum,16);

System.out.println("Decimal equivalent of given hex number: "+num);
}
}``````

7. By S.J LEE

Made by S.J LEE. ( Source )

``````import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner c=new Scanner(System.in);
String x=c.next();
int n=x.length(),res=0;
for(int i=0;i<n;i++){
int tmp,k=n-i-1;
String t=x.substring(i,i+1);
if(t.equals("A")) tmp=10;
else if(t.equals("B")) tmp=11;
else if(t.equals("C")) tmp=12;
else if(t.equals("D")) tmp=13;
else if(t.equals("E")) tmp=14;
else if(t.equals("F")) tmp=15;
else tmp=Integer.parseInt(t);
while(k--!=0) tmp*=16;
res+=tmp;
}
System.out.printf("%d",res);
}
}``````

8. By N.Venkatesh

Made by N.Venkatesh. Basic Java Hexadecimal to decimal converter. ( Source )

``````import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
String s=x.nextLine();
int n=0,t=0;
int l=s.length();
for(int i=l-1;i>=0;i--)
{
switch(s.charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':t=s.charAt(i)-48;break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':t=s.charAt(i)-55;break;
default:
System.out.print("invalid data");
}
n+=t*Math.pow(16,l-i-1);
}
}
}``````

9. By Jnanendra Mendi

Made by Jnanendra Mendi. ( Source )

``````import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
String s=x.nextLine();
int n=0,t=0;
int l=s.length();
for(int i=l-1;i>=0;i--)
{
switch(s.charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':t=s.charAt(i)-48;break;
case 'A':t=10;break;
case 'B':t=11;break;
case 'C':t=12;break;
case 'D':t=13;break;
case 'E':t=14;break;
case 'F':t=15;break;
default:
System.out.println("invalid data");
}
n+=t*Math.pow(16,l-i-1);
}
}
}``````

10. By Deroman

Made by Deroman. ( Source )

``````public class Program {

public static int hexToDec (String hex) {

int i;

String bin = "";

for (i=0;i<hex.length();i++) {

switch (hex.charAt(i)) {
case '0':
bin += "0000";
break;

case '1':
bin += "0001";
break;

case '2':
bin += "0010";
break;

case '3':
bin += "0011";
break;

case '4':
bin += "0100";
break;

case '5':
bin += "0101";
break;

case '6':
bin += "0110";
break;

case '7':
bin += "0111";
break;

case '8':
bin += "1000";
break;

case '9':
bin += "1001";
break;

case 'A':
case 'a':
bin += "1010";
break;

case 'B':
case 'b':
bin += "1011";
break;

case 'C':
case 'c':
bin += "1100";
break;

case 'D':
case 'd':
bin += "1101";
break;

case 'E':
case 'e':
bin += "1110";
break;

case 'F':
case 'f':
bin += "1111";
break;
}
}

int dec = 0;

for (i=bin.length()-1; i>=0; i--) {
if (bin.charAt(i) == '1'){
dec += Math.pow(2,bin.length()-1-i);
}
}

return dec;
}

public static void main(String[] args) {

String hex = "D90a";

int dec = hexToDec(hex);

System.out.println(dec);
}
}``````

11. By Prantik Sarkar

Made by Prantik Sarkar. Program to Convert hexadecimal to decimal Example: Input: Entered Hexadecimal: A, Output: Decimal value is: 10 ( Source )

``````import java.util.Scanner;
public class HexToDec {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String hex = sc.nextLine();
int dec = 0;
int x = 1;
for (int i = hex.length() - 1; i >= 0; i--) {
int temp = convert(hex.charAt(i));
dec = dec + (temp * x);
x = x * 16;
}
System.out.println("Decimal value is: " + dec);
}
private static int convert(char ch) {
int num = 0;
if (ch >= 65 && ch <= 70)
num = (int) ch - 55;
else
num = (int) ch - 48;
return num;
}
}//end of class``````

12. By Areesh Zafar

Made by Areesh Zafar. ( Source )

``````import java.util.*;
{
public static void main(String[]args)
{
Scanner sc= new Scanner(System.in);
String s=sc.nextLine();
int ns=0;
int l=s.length();
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch>='0'&&ch<='9')
{
int c=Integer.parseInt(""+ch);
ns=ns+(c*(int)Math.pow(16,(l-1-i)));
}
else if(ch>='A'&&ch<='F')
{
int c=((int)ch)-55;
ns=ns+((int)Math.pow(16,(l-i-1))*c);
}
}
System.out.println("decimal form of "+s+" is "+ns);
}
}``````

13. By Neil Bhowmik

Made by Neil Bhowmik. ( Source )

``````import java.util.Scanner;
import java.lang.Math;
class Hex
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
int e,g,d,f;
int b,c;
String str=sc.next();
f=0;
d=0;
g=16;
char[] arr=new char[str.length()];
arr=str.toCharArray();
for(b=arr.length-1;b>=0;b--)
{
switch(arr[b])
{
case '0':
c=0;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '1':
c=1;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '2':
c=2;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '3':
c=3;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '4':
c=4;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '5':
c=5;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '6':
c=6;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '7':
c=7;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '8':
c=8;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case '9':
c=9;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'A':
c=10;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'B':
c=11;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'C':
c=12;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'D':
c=13;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'E':
c=14;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
case 'F':
c=15;
e=c*(int)(Math.pow(g,d));
d++;
f=f+e;
break;
}

}
System.out.println("The decimal is:"+f);

}
}``````

14. By KANCHUPATI SAI PRASANNA

Made by KANCHUPATI SAI PRASANNA. ( Source )

``````import java.util.*;
public class Program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

String s1="1C4";

String s2="0123456789ABCDEF";

int l=s1.length();

int sum=0;
for(int i=0;i<l;i++)
{
sum=sum*16+s2.indexOf(s1.charAt(i));
}

System.out.println(sum);

}
}``````

15. By Tamanampudi Sri Pawan Kishore Reddy

Made by Tamanampudi Sri Pawan Kishore Reddy. ( Source )

``````import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
int i,k=0;

Scanner s=new Scanner(System.in);
String s1="1C4";
String s2="0123456789ABCDEF";

int j=s1.length()-1;
double s3;
double s4=0;
for(i=0;i<s1.length();i++)
{

s3=s2.indexOf(s1.charAt(i));

s4+=s3*Math.pow(16,j);

j--;

}
System.out.print(s4+" ");

}
}``````

16. By Taufique Sekh

Made by Taufique Sekh. ( Source )

``````class hexaToDecimal
{
static void h2D(String h)
{
int sum=0,c=0;
for(int i=h.length()-1;i>=0;i--)
{
char ch=h.charAt(i);
if(ch>=48 && ch<=57)
{
sum=sum+(ch-48)*pow(16,c);
}
else if(ch>=65 && ch <=90)
{
sum=sum+(ch-55)*pow(16,c);
}
else
{
sum=sum+(ch-87)*pow(16,c);
}
c++;
}
System.out.println("Decimal value is : "+sum);
}
static int pow(int n,int p)
{
int pw=1;
while(p>0)
{
pw=pw*n;
p--;
}
return pw;
}
public static void main(String[]args)
{
h2D("2d01");
}
}class octalToDecimal
{
static void o2D(int n)
{
int sum=0,c=0;
while(n>0)
{
int r=n%10;
sum=sum+r*pow(8,c);
n=n/10;
c++;
}
System.out.println("Decimal value is : "+sum);
}
static int pow(int n,int p)
{
int pw=1;
while(p>0)
{
pw=pw*n;
p--;
}
return pw;
}
public static void main(String[] args)
{
o2D(145);
}
}``````

17. By Barel Hatuka

Made by Barel Hatuka. ( Source )

``````public class Program
{
public static void main(String[] args) {
int number = convertHTD("80e1");
System.out.println(number);
}
public static int convertHTD(String str){
String hexaValues = "0123456789ABCDEF";
str = str.toUpperCase();
int length = str.length();
if (length > 0){
char nextValue = str.charAt(0);
int index = hexaValues.indexOf(nextValue);
return index * (int) Math.pow(16, length - 1) + convertHTD(str.substring(1));
}
return 0;
}
}``````

18. By GAURAV JAISWAL

Made by GAURAV JAISWAL. ( Source )

``````import java.util.Scanner;
public class Hex_To_Dec
{
public static void main(String args[])
{
int i,l,pos,p,n=0,v;
double d,s=0.0;
String no,chr="";
char j,ch;
boolean c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a hexadecimal number to convert to decimal :");
no = sc.nextLine();
l = no.length();
pos = no.indexOf('.');
p = l-1;
if(pos>0)
{
p = pos-1;
}
for(i=0;i<=l-1;i++,p--)
{
c = false;
if(i==pos)
{
p++;
continue;
}
ch = no.charAt(i);
if((ch>='A')&&(ch<='F'))
{
c=true;
for(j='A',v=10;j<='F';j++,v++)
{
if(ch==j)
n = v;
}
}
chr = chr + ch;
if(c==false)
{
n = Integer.parseInt(chr);
}
d = Math.pow(16,p);
s = s + (n * d);
chr = "";
}
if((int)s==s)
System.out.println((int)s);
else
System.out.println(s);
}
}
``````

19. By Sreerup

Made by Sreerup. Java code for converting hexadecimal value to decimal value. ( Source )

``````import java.util.Scanner;

public class HexToDeci {
public static void main(String args[]) {
String str="";
int n, rem, i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
n = sc.nextInt();
while(n>0) {
rem = n%16;
if(rem == 10) {
str = str + "A";
}
else if(rem == 11) {
str = str + "B";
}
else if(rem == 12) {
str = str + "C";
}
else if(rem == 13) {
str = str + "D";
}
else if(rem == 14) {
str = str + "E";
}
else if(rem == 15) {
str = str + "F";
}
else {
str = str + rem;
}
n = (int) n/16;
}
for(i=str.length()-1; i>=0; i--) {
System.out.print(str.charAt(i));
}
}
}``````

20. By JongHyunOppa

Made by JongHyunOppa. Converts hexadecimal number into decimal number and tests it in main method. ( Source )

``````import java.util.Scanner;
public class Week12_2
{
Scanner input = new Scanner(System.in);
System.out.println("Enter hex number: ");
String a = input.next();
HexToDec(a);

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter hex number: ");
String a = input.next();
HexToDec(a);
}
}``````

21. By NRK96

Made by NRK96. ( Source )

``````
/*
* Nicholas Keen
* CIS 356
* 9/14/2015
*
* Assignment 01
* File: H2D.java
*
*/

public class H2D {

public static int h2d(String h) {
// return the int d whose Hexadecimal representation
// is given by the String h
int d = 0;
String s = h.toUpperCase();
int len = s.length();
for(int i =0; i <= len-1; i++){
char c = s.charAt(i);
if(c >= 48 && c < 58){
d = (d*16)+(int) ((c - 48));
}else
d = (d*16) + (Math.abs('A' - c)+10);
}
return d; // dummy return
}

public static void main(String [] args) {
for (String s : args) {
int x = h2d(s);
System.out.println(s + ": " + x);
}
}
}``````