Skip to content

Commit a15a536

Browse files
tapankavasthiTapan Avasthi
andauthored
KTLN-641: Convert Octal Number to Decimal and Vice-Versa (#592)
Co-authored-by: Tapan Avasthi <[email protected]>
1 parent 4153a36 commit a15a536

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.math.octalconversion
2+
3+
import java.math.BigInteger
4+
5+
fun octalToDecimalUsingParseInt(octal: String): Int {
6+
return Integer.parseInt(octal, 8)
7+
}
8+
9+
fun octalToDecimalUsingIteration(octal: String): Int {
10+
var decimal = 0
11+
var power = 0
12+
for (i in octal.length - 1 downTo 0) {
13+
val digit = octal[i].toString().toInt()
14+
decimal += digit * Math.pow(8.0, power.toDouble()).toInt()
15+
power++
16+
}
17+
return decimal
18+
}
19+
20+
fun String.octalToDecimal(): Int {
21+
return Integer.parseInt(this, 8)
22+
}
23+
24+
fun octalToDecimalUsingToInt(octal: String): Int {
25+
return octal.toInt(8)
26+
}
27+
28+
fun octalToDecimalUsingBigInteger(octal: String): BigInteger {
29+
return BigInteger(octal, 8)
30+
}
31+
32+
fun decimalToOctalUsingIntegerToString(decimal: Int): String {
33+
return Integer.toString(decimal, 8)
34+
}
35+
36+
fun decimalToOctalUsingStringInterpolation(decimal: Int): String {
37+
return "%o".format(decimal)
38+
}
39+
40+
fun Int.decimalToOctal(): String {
41+
return Integer.toString(this, 8)
42+
}
43+
44+
fun decimalToOctalUsingIteration(decimal: Int): String {
45+
var octal = ""
46+
var num = decimal
47+
while (num > 0) {
48+
octal = (num % 8).toString() + octal
49+
num /= 8
50+
}
51+
return octal
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.math.octalconversion
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
import java.math.BigInteger
6+
7+
class OctalDecimalConverstionUnitTest {
8+
9+
@Test
10+
fun `test octal to decimal conversion using octalToDecimalUsingParseInt should give correct result`() {
11+
Assertions.assertEquals(28, octalToDecimalUsingParseInt("34"))
12+
}
13+
14+
@Test
15+
fun `test octal to decimal conversion using octalToDecimalUsingIteration should give correct result`() {
16+
Assertions.assertEquals(28, octalToDecimalUsingIteration("34"))
17+
}
18+
19+
@Test
20+
fun `test octal to decimal conversion using octalToDecimal extension function should give correct result`() {
21+
Assertions.assertEquals(28, "34".octalToDecimal())
22+
}
23+
24+
@Test
25+
fun `test octal to decimal conversion using octalToDecimalUsingToInt should give correct result`() {
26+
Assertions.assertEquals(28, octalToDecimalUsingToInt("34"))
27+
}
28+
29+
@Test
30+
fun `test octal to decimal conversion using octalToDecimalUsingBigInteger should give correct result`() {
31+
Assertions.assertEquals(BigInteger.valueOf(28), octalToDecimalUsingBigInteger("34"))
32+
}
33+
34+
@Test
35+
fun `test decimal to octal conversion using decimalToOctalUsingIntegerToString should give correct result`() {
36+
Assertions.assertEquals("34", decimalToOctalUsingIntegerToString(28))
37+
}
38+
39+
@Test
40+
fun `test decimal to octal conversion using decimalToOctalUsingStringInterpolation should give correct result`() {
41+
Assertions.assertEquals("34", decimalToOctalUsingStringInterpolation(28))
42+
}
43+
44+
@Test
45+
fun `test decimal to octal conversion using decimalToOctal extension function should give correct result`() {
46+
Assertions.assertEquals("34", 28.decimalToOctal())
47+
}
48+
49+
@Test
50+
fun `test decimal to octal conversion using decimalToOctalUsingIteration should give correct result`() {
51+
Assertions.assertEquals("34", decimalToOctalUsingIteration(28))
52+
}
53+
}

0 commit comments

Comments
 (0)