-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
HexToOct.java
62 lines (54 loc) · 1.59 KB
/
HexToOct.java
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
package com.thealgorithms.conversions;
/**
* Converts any Hexadecimal Number to Octal
*
* @author Tanmay Joshi
*/
public final class HexToOct {
private HexToOct() {
}
/**
* Converts a Hexadecimal number to a Decimal number.
*
* @param hex The Hexadecimal number as a String.
* @return The Decimal equivalent as an integer.
*/
public static int hexToDecimal(String hex) {
String hexDigits = "0123456789ABCDEF";
hex = hex.toUpperCase();
int decimalValue = 0;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
int digitValue = hexDigits.indexOf(hexChar);
decimalValue = 16 * decimalValue + digitValue;
}
return decimalValue;
}
/**
* Converts a Decimal number to an Octal number.
*
* @param decimal The Decimal number as an integer.
* @return The Octal equivalent as an integer.
*/
public static int decimalToOctal(int decimal) {
int octalValue = 0;
int placeValue = 1;
while (decimal > 0) {
int remainder = decimal % 8;
octalValue += remainder * placeValue;
decimal /= 8;
placeValue *= 10;
}
return octalValue;
}
/**
* Converts a Hexadecimal number to an Octal number.
*
* @param hex The Hexadecimal number as a String.
* @return The Octal equivalent as an integer.
*/
public static int hexToOctal(String hex) {
int decimalValue = hexToDecimal(hex);
return decimalToOctal(decimalValue);
}
}