Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6efaddb

Browse files
authored
Merge pull requestTheAlgorithms#316 from The-TJ/patch-1
Add Hexadecimal to Octal
2 parents3bfca30 +27839d2 commit6efaddb

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

‎Conversions/HexToOct.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
+ * Converts any Hexadecimal Number to Octal
3+
+ *
4+
+ * @author Tanmay Joshi
5+
+ *
6+
+ */
7+
importjava.util.Scanner;
8+
9+
publicclassHexToOct
10+
{
11+
/**
12+
+ * This method converts a Hexadecimal number to
13+
+ * a decimal number
14+
+ *
15+
+ * @param The Hexadecimal Number
16+
+ * @return The Decimal number
17+
+ */
18+
publicstaticinthex2decimal(Strings)
19+
{
20+
Stringstr ="0123456789ABCDEF";
21+
s =s.toUpperCase();
22+
intval =0;
23+
for (inti =0;i <s.length();i++)
24+
{
25+
chara =s.charAt(i);
26+
intn =str.indexOf(a);
27+
val =16*val +n;
28+
}
29+
returnval;
30+
}
31+
32+
/**
33+
+ * This method converts a Decimal number to
34+
+ * a octal number
35+
+ *
36+
+ * @param The Decimal Number
37+
+ * @return The Octal number
38+
+ */
39+
publicstaticintdecimal2octal(intq)
40+
{
41+
intnow;
42+
inti=1;
43+
intoctnum=0;
44+
while(q>0)
45+
{
46+
now=q%8;
47+
octnum=(now*(int)(Math.pow(10,i)))+octnum;
48+
q/=8;
49+
i++;
50+
}
51+
octnum/=10;
52+
returnoctnum;
53+
}
54+
// Main method that gets the hex input from user and converts it into octal.
55+
publicstaticvoidmain(Stringargs[])
56+
{
57+
Stringhexadecnum;
58+
intdecnum,octalnum;
59+
Scannerscan =newScanner(System.in);
60+
61+
System.out.print("Enter Hexadecimal Number : ");
62+
hexadecnum =scan.nextLine();
63+
64+
// first convert hexadecimal to decimal
65+
66+
decnum =hex2decimal(hexadecnum);//Pass the string to the hex2decimal function and get the decimal form in variable decnum
67+
68+
// convert decimal to octal
69+
octalnum=decimal2octal(decnum);
70+
System.out.println("Number in octal: "+octalnum);
71+
72+
73+
}
74+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp