forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalToOctal.java
More file actions
27 lines (23 loc) · 794 Bytes
/
DecimalToOctal.java
File metadata and controls
27 lines (23 loc) · 794 Bytes
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
package com.conversions;
import java.math.BigInteger;
public class DecimalToOctal {
private static final char[] octalChars = {'0', '1', '2', '3', '4', '5', '6', '7'};
private static final BigInteger valueOctal = BigInteger.valueOf(8);
/**
* This method converts and decimal number to a octal number
*
* @param decimalStr
* @return octal number
*/
public String decimalToOctal(String decimalStr) {
BigInteger decimal = new BigInteger(decimalStr);
int rem;
String octal = "";
while (decimal.compareTo(BigInteger.ZERO) > 0) {
rem = decimal.mod(valueOctal).intValueExact();
octal = octalChars[rem] + octal;
decimal = decimal.divide(valueOctal);
}
return octal;
}
}