Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db3f011

Browse files
Add missing octal conversions (TheAlgorithms#931)
1 parent b39484d commit db3f011

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

‎DIRECTORY.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@
5050
* Conversions
5151
* [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs)
5252
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs)
53+
* [Binary To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_octal.rs)
5354
* [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
5455
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_hexadecimal.rs)
56+
* [Decimal To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_octal.rs)
5557
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
5658
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs)
59+
* [Hexadecimal To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_octal.rs)
5760
* [Length Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/length_conversion.rs)
5861
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
5962
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
63+
* [Octal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs)
6064
* [Rgb Cmyk Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
6165
* Data Structures
6266
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)

‎src/conversions/binary_to_octal.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Author: NithinU2802
2+
// Binary to Octal Converter: Converts Binary to Octal
3+
// Wikipedia References:
4+
// 1. https://en.wikipedia.org/wiki/Binary_number
5+
// 2. https://en.wikipedia.org/wiki/Octal
6+
7+
pub fn binary_to_octal(binary_str: &str) -> Result<String, &'static str> {
8+
// Validate input
9+
let binary_str = binary_str.trim();
10+
if binary_str.is_empty() {
11+
return Err("Empty string");
12+
}
13+
14+
if !binary_str.chars().all(|c| c == '0' || c == '1') {
15+
return Err("Invalid binary string");
16+
}
17+
18+
// Pad the binary string with zeros to make its length a multiple of 3
19+
let padding_length = (3 - (binary_str.len() % 3)) % 3;
20+
let padded_binary = "0".repeat(padding_length) + binary_str;
21+
22+
// Convert every 3 binary digits to one octal digit
23+
let mut octal = String::new();
24+
for chunk in padded_binary.chars().collect::<Vec<char>>().chunks(3) {
25+
let binary_group: String = chunk.iter().collect();
26+
let decimal = u8::from_str_radix(&binary_group, 2).map_err(|_| "Conversion error")?;
27+
octal.push_str(&decimal.to_string());
28+
}
29+
30+
Ok(octal)
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn test_binary_to_octal() {
39+
assert_eq!(binary_to_octal("1010"), Ok("12".to_string()));
40+
assert_eq!(binary_to_octal("1111"), Ok("17".to_string()));
41+
assert_eq!(binary_to_octal("11111111"), Ok("377".to_string()));
42+
assert_eq!(binary_to_octal("1100100"), Ok("144".to_string()));
43+
}
44+
45+
#[test]
46+
fn test_invalid_input() {
47+
assert_eq!(binary_to_octal(""), Err("Empty string"));
48+
assert_eq!(binary_to_octal("12"), Err("Invalid binary string"));
49+
assert_eq!(binary_to_octal("abc"), Err("Invalid binary string"));
50+
}
51+
}

‎src/conversions/decimal_to_octal.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Author: NithinU2802
2+
// Decimal to Octal Converter: Converts Decimal to Octal
3+
// Wikipedia References:
4+
// 1. https://en.wikipedia.org/wiki/Decimal
5+
// 2. https://en.wikipedia.org/wiki/Octal
6+
7+
pub fn decimal_to_octal(decimal_num: u64) -> String {
8+
if decimal_num == 0 {
9+
return "0".to_string();
10+
}
11+
12+
let mut num = decimal_num;
13+
let mut octal = String::new();
14+
15+
while num > 0 {
16+
let remainder = num % 8;
17+
octal.push_str(&remainder.to_string());
18+
num /= 8;
19+
}
20+
21+
// Reverse the string to get the correct octal representation
22+
octal.chars().rev().collect()
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_decimal_to_octal() {
31+
assert_eq!(decimal_to_octal(8), "10");
32+
assert_eq!(decimal_to_octal(15), "17");
33+
assert_eq!(decimal_to_octal(255), "377");
34+
assert_eq!(decimal_to_octal(100), "144");
35+
assert_eq!(decimal_to_octal(0), "0");
36+
}
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Author: NithinU2802
2+
// Hexadecimal to Octal Converter: Converts Hexadecimal to Octal
3+
// Wikipedia References:
4+
// 1. https://en.wikipedia.org/wiki/Hexadecimal
5+
// 2. https://en.wikipedia.org/wiki/Octal
6+
7+
pub fn hexadecimal_to_octal(hex_str: &str) -> Result<String, &'static str> {
8+
let hex_str = hex_str.trim();
9+
10+
if hex_str.is_empty() {
11+
return Err("Empty string");
12+
}
13+
14+
// Validate hexadecimal string
15+
if !hex_str.chars().all(|c| c.is_ascii_hexdigit()) {
16+
return Err("Invalid hexadecimal string");
17+
}
18+
19+
// Convert hex to decimal first
20+
let decimal = u64::from_str_radix(hex_str, 16).map_err(|_| "Conversion error")?;
21+
22+
// Then convert decimal to octal
23+
if decimal == 0 {
24+
return Ok("0".to_string());
25+
}
26+
27+
let mut num = decimal;
28+
let mut octal = String::new();
29+
30+
while num > 0 {
31+
let remainder = num % 8;
32+
octal.push_str(&remainder.to_string());
33+
num /= 8;
34+
}
35+
36+
// Reverse the string to get the correct octal representation
37+
Ok(octal.chars().rev().collect())
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_hexadecimal_to_octal() {
46+
assert_eq!(hexadecimal_to_octal("A"), Ok("12".to_string()));
47+
assert_eq!(hexadecimal_to_octal("FF"), Ok("377".to_string()));
48+
assert_eq!(hexadecimal_to_octal("64"), Ok("144".to_string()));
49+
assert_eq!(hexadecimal_to_octal("0"), Ok("0".to_string()));
50+
}
51+
52+
#[test]
53+
fn test_invalid_input() {
54+
assert_eq!(hexadecimal_to_octal(""), Err("Empty string"));
55+
assert_eq!(
56+
hexadecimal_to_octal("GG"),
57+
Err("Invalid hexadecimal string")
58+
);
59+
assert_eq!(
60+
hexadecimal_to_octal("XYZ"),
61+
Err("Invalid hexadecimal string")
62+
);
63+
}
64+
}

‎src/conversions/mod.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
mod binary_to_decimal;
22
mod binary_to_hexadecimal;
3+
mod binary_to_octal;
34
mod decimal_to_binary;
45
mod decimal_to_hexadecimal;
6+
mod decimal_to_octal;
57
mod hexadecimal_to_binary;
68
mod hexadecimal_to_decimal;
9+
mod hexadecimal_to_octal;
710
mod length_conversion;
811
mod octal_to_binary;
912
mod octal_to_decimal;
13+
mod octal_to_hexadecimal;
1014
mod rgb_cmyk_conversion;
1115
pub use self::binary_to_decimal::binary_to_decimal;
1216
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
17+
pub use self::binary_to_octal::binary_to_octal;
1318
pub use self::decimal_to_binary::decimal_to_binary;
1419
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
20+
pub use self::decimal_to_octal::decimal_to_octal;
1521
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1622
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
23+
pub use self::hexadecimal_to_octal::hexadecimal_to_octal;
1724
pub use self::length_conversion::length_conversion;
1825
pub use self::octal_to_binary::octal_to_binary;
1926
pub use self::octal_to_decimal::octal_to_decimal;
27+
pub use self::octal_to_hexadecimal::octal_to_hexadecimal;
2028
pub use self::rgb_cmyk_conversion::rgb_to_cmyk;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Author: NithinU2802
2+
// Octal to Hexadecimal Converter: Converts Octal to Hexadecimal
3+
// Wikipedia References:
4+
// 1. https://en.wikipedia.org/wiki/Octal
5+
// 2. https://en.wikipedia.org/wiki/Hexadecimal
6+
7+
pub fn octal_to_hexadecimal(octal_str: &str) -> Result<String, &'static str> {
8+
let octal_str = octal_str.trim();
9+
10+
if octal_str.is_empty() {
11+
return Err("Empty string");
12+
}
13+
14+
// Validate octal string
15+
if !octal_str.chars().all(|c| ('0'..='7').contains(&c)) {
16+
return Err("Invalid octal string");
17+
}
18+
19+
// Convert octal to decimal first
20+
let decimal = u64::from_str_radix(octal_str, 8).map_err(|_| "Conversion error")?;
21+
22+
// Special case for zero
23+
if decimal == 0 {
24+
return Ok("0".to_string());
25+
}
26+
27+
// Convert decimal to hexadecimal
28+
Ok(format!("{decimal:X}"))
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn test_octal_to_hexadecimal() {
37+
assert_eq!(octal_to_hexadecimal("12"), Ok("A".to_string()));
38+
assert_eq!(octal_to_hexadecimal("377"), Ok("FF".to_string()));
39+
assert_eq!(octal_to_hexadecimal("144"), Ok("64".to_string()));
40+
assert_eq!(octal_to_hexadecimal("0"), Ok("0".to_string()));
41+
}
42+
43+
#[test]
44+
fn test_invalid_input() {
45+
assert_eq!(octal_to_hexadecimal(""), Err("Empty string"));
46+
assert_eq!(octal_to_hexadecimal("8"), Err("Invalid octal string"));
47+
assert_eq!(octal_to_hexadecimal("9"), Err("Invalid octal string"));
48+
assert_eq!(octal_to_hexadecimal("ABC"), Err("Invalid octal string"));
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /