Skip to content

Commit 12e7f66

Browse files
author
Christian Bender
authored
Merge pull request #333 from The-TJ/patch-2
Thanks for contribution
2 parents fe78789 + b190206 commit 12e7f66

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Conversions/OctalToHexadecimal.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
+ + * Converts any Octal Number to HexaDecimal
3+
+ + *
4+
+ + * @author Tanmay Joshi
5+
+ + *
6+
+ + *
7+
**/
8+
import java.util.Scanner;
9+
10+
public class OctalToHexadecimal {
11+
12+
/**
13+
+ + * This method converts a Octal number to
14+
+ + * a decimal number
15+
+ + *
16+
+ + * @param The Octal Number
17+
+ + * @return The Decimal number
18+
+ + */
19+
public static int OctToDec(String s)
20+
{
21+
int i =0;
22+
for(int j =0;j<s.length();j++)
23+
{
24+
char num = s.charAt(j);
25+
num-='0';
26+
i*=8;
27+
i+=num;
28+
}
29+
return i;
30+
}
31+
32+
/**
33+
+ + * This method converts a Decimal number to
34+
+ + * a Hexadecimal number
35+
+ + *
36+
+ + * @param The Decimal Number
37+
+ + * @return The Hexadecimal number
38+
+ + */
39+
public static String DecimalToHex(int d) {
40+
String digits = "0123456789ABCDEF";
41+
if (d <= 0)
42+
return "0";
43+
String hex = "";
44+
while (d > 0) {
45+
int digit = d % 16;
46+
hex = digits.charAt(digit) + hex;
47+
d = d / 16;
48+
}
49+
return hex;
50+
}
51+
52+
//Driver Program
53+
public static void main ( String args[]) {
54+
55+
Scanner input = new Scanner(System.in);
56+
System.out.print("Enter the Octal number: ");
57+
String oct = input.next(); //Take octal number as input from user in a string
58+
int decimal = OctToDec(oct); //Pass the octal number to function and get converted deciaml form
59+
String hex = DecimalToHex(decimal); //Pass the decimla number to function and get converted Hex form of the number
60+
System.out.println("The Hexadecimal equivalant is: "+hex);
61+
}
62+
}
63+

0 commit comments

Comments
 (0)