FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/HexToOct.java at master · rcfgnu/Java · GitHub
rcfgnu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Conversions
/
HexToOct.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (66 loc) · 1.64 KB
Breadcrumbs
Java
/
Conversions
/
HexToOct.java
Copy path
File metadata and controls
73 lines (66 loc) · 1.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package
Conversions
;
import
java
.
util
.
Scanner
;
/**
* Converts any Hexadecimal Number to Octal
*
* @author Tanmay Joshi
*/
public
class
HexToOct
{
/**
* This method converts a Hexadecimal number to a decimal number
*
* @param s The Hexadecimal Number
* @return The Decimal number
*/
public
static
int
hex2decimal
(
String
s
) {
String
str
=
"0123456789ABCDEF"
;
s
=
s
.
toUpperCase
();
int
val
=
0
;
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++) {
char
a
=
s
.
charAt
(
i
);
int
n
=
str
.
indexOf
(
a
);
val
=
16
*
val
+
n
;
}
return
val
;
}
/**
* This method converts a Decimal number to a octal number
*
* @param q The Decimal Number
* @return The Octal number
*/
public
static
int
decimal2octal
(
int
q
) {
int
now
;
int
i
=
1
;
int
octnum
=
0
;
while
(
q
>
0
) {
now
=
q
%
8
;
octnum
= (
now
* (
int
) (
Math
.
pow
(
10
,
i
))) +
octnum
;
q
/=
8
;
i
++;
}
octnum
/=
10
;
return
octnum
;
}
/**
* Main method that gets the hex input from user and converts it into octal.
*
* @param args arguments
*/
public
static
void
main
(
String
args
[]) {
String
hexadecnum
;
int
decnum
,
octalnum
;
Scanner
scan
=
new
Scanner
(
System
.
in
);
System
.
out
.
print
(
"Enter Hexadecimal Number : "
);
hexadecnum
=
scan
.
nextLine
();
// first convert hexadecimal to decimal
decnum
=
hex2decimal
(
hexadecnum
);
// Pass the string to the hex2decimal function and get the decimal form in
// variable decnum
// convert decimal to octal
octalnum
=
decimal2octal
(
decnum
);
System
.
out
.
println
(
"Number in octal: "
+
octalnum
);
scan
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL