Java Program to convert Decimal to Hexadecimal vice versa Hexadecimal to Decimal. Here we have written the code in three different ways using arrays, static method, recursion, vice versa with sample output and an example program. Before getting into the programming a couple of lines just to let you know the basics.
- What is Decimal?
A decimal number system is a term with base 10. It’s most commonly used number system. The decimal number system consists of 10 digits from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
- What is Hexadecimal?
Same as like: but with a different numbering system:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- How to convert then?
Conversion is quite simple: Just look at the Image here:
- Example: Convert the number 256 from decimal to hexadecimal
- Example – 2: Convert 188:
1. Using Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class DecimalToHexa { public static void main(String args[]) { char ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int rem,num; String hexadecimal=""; Scanner sc = new Scanner(System.in); System.out.println("Enter a Decimal number: "); num=sc.nextInt(); System.out.println("Hexadecimal number is : "); while(num != 0) { rem=num%16; hexadecimal= ch[rem] + hexadecimal; num= num/16; } System.out.print(hexadecimal); } } |
Output:
1 2 3 4 |
Enter a Decimal number: 182 Hexadecimal number is : B6 |
2. Static Method
Another example program with sample output: #Static Method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class DecimalToHexa { static int i=1; public static void main(String args[]) { int decimal, rem, q,j=1; Scanner sc = new Scanner(System.in); System.out.println("Enter a Decimal number: "); decimal=sc.nextInt(); System.out.println("Hexadecimal number is : "); String hex=hexadecimal(decimal); System.out.print(hex); } static String hexadecimal(int q) { char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int rem; String hexdec=""; while(q != 0) { rem=q%16; hexdec= a[rem] + hexdec; q= q/16; } return hexdec; } } |
Output:
1 2 3 4 |
Enter a Decimal number: 45 Hexadecimal number is : 2D |
3. Using Recursion
- Another example program using recursion: A function call itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class DecimalToHexa { char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int rem; String hexdec=""; String hexadecimal(int q) { if(q != 0) { rem=q%16; hexdec= a[rem] + hexdec; q= q/16; hexadecimal(q); } return hexdec; } public static void main(String args[]) { DecimalToHexa d=new DecimalToHexa(); int dec, rem, q; Scanner sc = new Scanner(System.in); System.out.println("Enter a Decimal number: "); dec=sc.nextInt(); System.out.println("Hexadecimal number is : "); String h=d.hexadecimal(dec); System.out.print(h); } } |
Output:
1 2 3 4 |
Enter a Decimal number: 125 Hexadecimal number is : 7D |
4. Vice versa: convert hexadecimal to Decimal
Here is the Java code program conversion from Hexadecimal to decimal with sample outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class HexaDecimalToDecimal { public static void main(String args[]) { String hexdec ; String hex= "0123456789ABCDEF"; Scanner sc = new Scanner(System.in); System.out.print("Enter Hexadecimal Number : "); hexdec = sc.nextLine(); hexdec = hexdec.toUpperCase(); int decimal = 0; for (int i = 0; i < hexdec.length(); i++) { char ch = hexdec.charAt(i); int in= hex.indexOf(ch); decimal = 16*decimal + in; } System.out.print("Decimal Number is " + decimal); } } |
Output:
1 2 |
Enter Hexadecimal Number : F Decimal Number is 15 |
List of other programs: