Skip to content

Commit 37838f6

Browse files
authored
Create HextoDec.java
Program that converts Hexadecimal numbers to decimal.
1 parent df6838e commit 37838f6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Conversions/HexToOct.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Java Program - Convert Hexadecimal to Octal
2+
Author - Tanmay Joshi*/
3+
4+
import java.util.Scanner;
5+
6+
public class HexToOct
7+
{
8+
public static int hex2decimal(String s)
9+
{
10+
String str = "0123456789ABCDEF";
11+
s = s.toUpperCase();
12+
int val = 0;
13+
for (int i = 0; i < s.length(); i++)
14+
{
15+
char a = s.charAt(i);
16+
int n = str.indexOf(a);
17+
val = 16*val + n;
18+
}
19+
return val;
20+
}
21+
public static void main(String args[])
22+
{
23+
String hexadecnum;
24+
int decnum, i=1, j;
25+
int octnum[] = new int[100];
26+
Scanner scan = new Scanner(System.in);
27+
28+
System.out.print("Enter Hexadecimal Number : ");
29+
hexadecnum = scan.nextLine();
30+
31+
// first convert hexadecimal to decimal
32+
33+
decnum = hex2decimal(hexadecnum);
34+
35+
// convert decimal to octal
36+
37+
while(decnum != 0)
38+
{
39+
octnum[i++] = decnum%8;
40+
decnum = decnum/8;
41+
}
42+
43+
System.out.print("Equivalent Octal Number is :\n");
44+
for(j=i-1; j>0; j--)
45+
{
46+
System.out.print(octnum[j]);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)