Java program to convert octal to hexadecimal using standard values, recursion, using an array and vice versa ( convert from hexadecimal to octal ( vice versa ) with sample outputs and example programs. The following program has been written in three formats and also vice versa.
In order to get into programming part Just a couple of lines to let you know what and how to calculate octal and hexadecimal in math.
- What is octal?
An octal is a computer-based system with base eight. The digits in math are 0, 1, 2, 3, 4, 5, 6, 7. The value to the base eight represents as single 8 or Zero or 108.
- What is hexadecimal?
Hexadecimal is base 16 number system. The hexadecimal is different represent system when compare with the base 10 or octal. The representation of hexadecimal as follows:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- How to convert Octal to hex?
A: Here is the procedure: conversion,
If you clearly look into the above example you will get to know the method of conversion.
Hope you get it. Here is the complete source code for the above program and vice versa.
Conversion From Octal To Hexadecimal Java Program
1. Using standard Method
[wp_ad_camp_3]
Here we go standard method with sample outputs, as we do already share the conversion you will get to know how this program works. For example, if the number to the base 8 ( octal ) numbers is 16. Than 16 represent as “E” same as followed by 17 represent as ” F “, octal number 18 convert to hexa 10. That’s it
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 31 32 |
import java.util.Scanner; class OctalToHexa { public static void main(String arg[]) { int oct, dec=0, i=0, t; Scanner sc = new Scanner(System.in); System.out.println("Enter Octl Number : "); oct = sc.nextInt(); while(oct != 0) { dec =dec + (oct%10) *(int)Math.pow(8, i); i++; oct = oct/10; } String hex=hexdecimal(dec); System.out.println("Hexdecimal number is :"+hex); } static String hexdecimal(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 Octl Number : 16 Hexdecimal number is : E |
1 2 3 4 |
Enter Octl Number : 18 Hexdecimal number is : 10 |
2. Using Recursion
There you go another sample method of using recursion with sample outputs to print octal to hexadecimal.
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 31 32 33 34 35 36 |
import java.util.Scanner; class OctalToHexa { 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 arg[]) { OctalToHexa var=new OctalToHexa(); int oct, decimal=0, i=0, t; Scanner sc = new Scanner(System.in); System.out.println("Enter Octal Number : "); oct = sc.nextInt(); while(oct != 0) { decimal =decimal + (oct%10) *(int)Math.pow(8, i); i++; oct = oct/10; } String hex=var.hexadecimal(decimal); System.out.println("Hexadecimal number is :"+hex); } } |
Output:
1 2 3 |
Enter Octal Number : 100 Hexadecimal number is :40 |
3. Convert Octal To Hexadecimal Using Array
Another sample program using array’s: An array is nothing but the continuous allocation of memory. The length of an array is established when an array is created.
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 |
import java.util.Scanner; class OctalToHexa { public static void main(String arg[]) { int oct, dec=0, i=0, t,rem; char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; String hexdec=""; Scanner sc = new Scanner(System.in); System.out.println("Enter Octal Number : "); oct = sc.nextInt(); while(oct != 0) { dec =dec + (oct%10) *(int)Math.pow(8, i); i++; oct = oct/10; } while(dec!= 0) { rem=dec%16; hexdec= a[rem] + hexdec; dec= dec/16; } System.out.println("Hexadecimal number is :"+hexdec); } } |
Output:
1 2 3 |
Enter Octal Number : 100 Hexadecimal number is :40 |
4. Conversion from hexadecimal to octal
Here is the program vice versa: Conversion from hexadecimal to octal with sample outputs and examples.
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 31 32 33 |
import java.util.Scanner; class HexaDecimalToOctal { 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.println("Decimal Number is : " + decimal); System.out.print("Octal number is : "); int i=0; int a[] = new int[50]; while(decimal != 0) { a[i++] = decimal%8; decimal= decimal/8; } for(int c=i-1;c>=0;c--) { System.out.print(a[c]); } } } |
Output:
1 2 3 |
Enter Hexadecimal Number : 100 Decimal Number is : 256 Octal number is : 400 |