Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathHexToOct.java
More file actions
Latest commit
62 lines (54 loc) · 1.59 KB
/
Copy pathHexToOct.java
File metadata and controls
62 lines (54 loc) · 1.59 KB
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
packagecom.thealgorithms.conversions;
/**
* Converts any Hexadecimal Number to Octal
*
* @author Tanmay Joshi
*/
publicfinalclassHexToOct {
privateHexToOct() {
}
/**
* Converts a Hexadecimal number to a Decimal number.
*
* @param hex The Hexadecimal number as a String.
* @return The Decimal equivalent as an integer.
*/
publicstaticinthexToDecimal(Stringhex) {
StringhexDigits = "0123456789ABCDEF";
hex = hex.toUpperCase();
intdecimalValue = 0;
for (inti = 0; i < hex.length(); i++) {
charhexChar = hex.charAt(i);
intdigitValue = hexDigits.indexOf(hexChar);
decimalValue = 16 * decimalValue + digitValue;
}
returndecimalValue;
}
/**
* Converts a Decimal number to an Octal number.
*
* @param decimal The Decimal number as an integer.
* @return The Octal equivalent as an integer.
*/
publicstaticintdecimalToOctal(intdecimal) {
intoctalValue = 0;
intplaceValue = 1;
while (decimal > 0) {
intremainder = decimal % 8;
octalValue += remainder * placeValue;
decimal /= 8;
placeValue *= 10;
}
returnoctalValue;
}
/**
* Converts a Hexadecimal number to an Octal number.
*
* @param hex The Hexadecimal number as a String.
* @return The Octal equivalent as an integer.
*/
publicstaticinthexToOctal(Stringhex) {
intdecimalValue = hexToDecimal(hex);
returndecimalToOctal(decimalValue);
}
}