Skip to content

Commit 3cc18de

Browse files
authored
Add Octal to Decimal Conversion (TheAlgorithms#605)
1 parent e7888f4 commit 3cc18de

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [Decimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
4646
* [Hexadecimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
4747
* [Octal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
48+
* [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
4849
* Data Structures
4950
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
5051
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)

src/conversions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ mod binary_to_hexadecimal;
33
mod decimal_to_binary;
44
mod hexadecimal_to_binary;
55
mod octal_to_binary;
6+
mod octal_to_decimal;
67
pub use self::binary_to_decimal::binary_to_decimal;
78
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
89
pub use self::decimal_to_binary::decimal_to_binary;
910
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1011
pub use self::octal_to_binary::octal_to_binary;
12+
pub use self::octal_to_decimal::octal_to_decimal;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Author: cyrixninja
2+
// Octal to Decimal Converter: Converts Octal to Decimal
3+
// Wikipedia References:
4+
// 1. https://en.wikipedia.org/wiki/Octal
5+
// 2. https://en.wikipedia.org/wiki/Decimal
6+
7+
pub fn octal_to_decimal(octal_str: &str) -> Result<u64, &'static str> {
8+
let octal_str = octal_str.trim();
9+
10+
if octal_str.is_empty() {
11+
return Err("Empty");
12+
}
13+
14+
if !octal_str.chars().all(|c| ('0'..='7').contains(&c)) {
15+
return Err("Non-octal Value");
16+
}
17+
18+
// Convert octal to decimal and directly return the Result
19+
u64::from_str_radix(octal_str, 8).map_err(|_| "Conversion error")
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_empty_string() {
28+
let input = "";
29+
let expected = Err("Empty");
30+
assert_eq!(octal_to_decimal(input), expected);
31+
}
32+
33+
#[test]
34+
fn test_invalid_octal() {
35+
let input = "89";
36+
let expected = Err("Non-octal Value");
37+
assert_eq!(octal_to_decimal(input), expected);
38+
}
39+
40+
#[test]
41+
fn test_valid_octal() {
42+
let input = "123";
43+
let expected = Ok(83);
44+
assert_eq!(octal_to_decimal(input), expected);
45+
}
46+
47+
#[test]
48+
fn test_valid_octal2() {
49+
let input = "1234";
50+
let expected = Ok(668);
51+
assert_eq!(octal_to_decimal(input), expected);
52+
}
53+
54+
#[test]
55+
fn test_valid_octal3() {
56+
let input = "12345";
57+
let expected = Ok(5349);
58+
assert_eq!(octal_to_decimal(input), expected);
59+
}
60+
}

0 commit comments

Comments
 (0)