From 90faee143fe709e82eddec68356895ef89256776 Mon Sep 17 00:00:00 2001 From: NithinU2802 Date: Fri, 24 Oct 2025 22:23:48 +0530 Subject: [PATCH 1/2] feat: to add missing octal conversions --- DIRECTORY.md | 4 ++ src/conversions/binary_to_octal.rs | 51 ++++++++++++++++++++ src/conversions/decimal_to_octal.rs | 37 ++++++++++++++ src/conversions/hexadecimal_to_octal.rs | 64 +++++++++++++++++++++++++ src/conversions/mod.rs | 8 ++++ src/conversions/octal_to_hexadecimal.rs | 50 +++++++++++++++++++ 6 files changed, 214 insertions(+) create mode 100644 src/conversions/binary_to_octal.rs create mode 100644 src/conversions/decimal_to_octal.rs create mode 100644 src/conversions/hexadecimal_to_octal.rs create mode 100644 src/conversions/octal_to_hexadecimal.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index 564a7813807..fc3b7f61941 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -50,13 +50,17 @@ * Conversions * [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs) * [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs) + * [Binary To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_octal.rs) * [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs) * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_hexadecimal.rs) + * [Decimal To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_octal.rs) * [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs) * [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs) + * [Hexadecimal To Octal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_octal.rs) * [Length Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/length_conversion.rs) * [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs) * [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs) + * [Octal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_hexadecimal.rs) * [Rgb Cmyk Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs) * Data Structures * [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs) diff --git a/src/conversions/binary_to_octal.rs b/src/conversions/binary_to_octal.rs new file mode 100644 index 00000000000..1a936afb3b6 --- /dev/null +++ b/src/conversions/binary_to_octal.rs @@ -0,0 +1,51 @@ +// Author: NithinU2802 +// Binary to Octal Converter: Converts Binary to Octal +// Wikipedia References: +// 1. https://en.wikipedia.org/wiki/Binary_number +// 2. https://en.wikipedia.org/wiki/Octal + +pub fn binary_to_octal(binary_str: &str) -> Result { + // Validate input + let binary_str = binary_str.trim(); + if binary_str.is_empty() { + return Err("Empty string"); + } + + if !binary_str.chars().all(|c| c == '0' || c == '1') { + return Err("Invalid binary string"); + } + + // Pad the binary string with zeros to make its length a multiple of 3 + let padding_length = (3 - (binary_str.len() % 3)) % 3; + let padded_binary = "0".repeat(padding_length) + binary_str; + + // Convert every 3 binary digits to one octal digit + let mut octal = String::new(); + for chunk in padded_binary.chars().collect::>().chunks(3) { + let binary_group: String = chunk.iter().collect(); + let decimal = u8::from_str_radix(&binary_group, 2).map_err(|_| "Conversion error")?; + octal.push_str(&decimal.to_string()); + } + + Ok(octal) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_binary_to_octal() { + assert_eq!(binary_to_octal("1010"), Ok("12".to_string())); + assert_eq!(binary_to_octal("1111"), Ok("17".to_string())); + assert_eq!(binary_to_octal("11111111"), Ok("377".to_string())); + assert_eq!(binary_to_octal("1100100"), Ok("144".to_string())); + } + + #[test] + fn test_invalid_input() { + assert_eq!(binary_to_octal(""), Err("Empty string")); + assert_eq!(binary_to_octal("12"), Err("Invalid binary string")); + assert_eq!(binary_to_octal("abc"), Err("Invalid binary string")); + } +} diff --git a/src/conversions/decimal_to_octal.rs b/src/conversions/decimal_to_octal.rs new file mode 100644 index 00000000000..37659017c83 --- /dev/null +++ b/src/conversions/decimal_to_octal.rs @@ -0,0 +1,37 @@ +// Author: NithinU2802 +// Decimal to Octal Converter: Converts Decimal to Octal +// Wikipedia References: +// 1. https://en.wikipedia.org/wiki/Decimal +// 2. https://en.wikipedia.org/wiki/Octal + +pub fn decimal_to_octal(decimal_num: u64) -> String { + if decimal_num == 0 { + return "0".to_string(); + } + + let mut num = decimal_num; + let mut octal = String::new(); + + while num > 0 { + let remainder = num % 8; + octal.push_str(&remainder.to_string()); + num /= 8; + } + + // Reverse the string to get the correct octal representation + octal.chars().rev().collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_decimal_to_octal() { + assert_eq!(decimal_to_octal(8), "10"); + assert_eq!(decimal_to_octal(15), "17"); + assert_eq!(decimal_to_octal(255), "377"); + assert_eq!(decimal_to_octal(100), "144"); + assert_eq!(decimal_to_octal(0), "0"); + } +} diff --git a/src/conversions/hexadecimal_to_octal.rs b/src/conversions/hexadecimal_to_octal.rs new file mode 100644 index 00000000000..2c60530248b --- /dev/null +++ b/src/conversions/hexadecimal_to_octal.rs @@ -0,0 +1,64 @@ +// Author: NithinU2802 +// Hexadecimal to Octal Converter: Converts Hexadecimal to Octal +// Wikipedia References: +// 1. https://en.wikipedia.org/wiki/Hexadecimal +// 2. https://en.wikipedia.org/wiki/Octal + +pub fn hexadecimal_to_octal(hex_str: &str) -> Result { + let hex_str = hex_str.trim(); + + if hex_str.is_empty() { + return Err("Empty string"); + } + + // Validate hexadecimal string + if !hex_str.chars().all(|c| c.is_digit(16)) { + return Err("Invalid hexadecimal string"); + } + + // Convert hex to decimal first + let decimal = u64::from_str_radix(hex_str, 16).map_err(|_| "Conversion error")?; + + // Then convert decimal to octal + if decimal == 0 { + return Ok("0".to_string()); + } + + let mut num = decimal; + let mut octal = String::new(); + + while num > 0 { + let remainder = num % 8; + octal.push_str(&remainder.to_string()); + num /= 8; + } + + // Reverse the string to get the correct octal representation + Ok(octal.chars().rev().collect()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_hexadecimal_to_octal() { + assert_eq!(hexadecimal_to_octal("A"), Ok("12".to_string())); + assert_eq!(hexadecimal_to_octal("FF"), Ok("377".to_string())); + assert_eq!(hexadecimal_to_octal("64"), Ok("144".to_string())); + assert_eq!(hexadecimal_to_octal("0"), Ok("0".to_string())); + } + + #[test] + fn test_invalid_input() { + assert_eq!(hexadecimal_to_octal(""), Err("Empty string")); + assert_eq!( + hexadecimal_to_octal("GG"), + Err("Invalid hexadecimal string") + ); + assert_eq!( + hexadecimal_to_octal("XYZ"), + Err("Invalid hexadecimal string") + ); + } +} diff --git a/src/conversions/mod.rs b/src/conversions/mod.rs index a83c46bf600..9ffcbfff4ec 100644 --- a/src/conversions/mod.rs +++ b/src/conversions/mod.rs @@ -1,20 +1,28 @@ mod binary_to_decimal; mod binary_to_hexadecimal; +mod binary_to_octal; mod decimal_to_binary; mod decimal_to_hexadecimal; +mod decimal_to_octal; mod hexadecimal_to_binary; mod hexadecimal_to_decimal; +mod hexadecimal_to_octal; mod length_conversion; mod octal_to_binary; mod octal_to_decimal; +mod octal_to_hexadecimal; mod rgb_cmyk_conversion; pub use self::binary_to_decimal::binary_to_decimal; pub use self::binary_to_hexadecimal::binary_to_hexadecimal; +pub use self::binary_to_octal::binary_to_octal; pub use self::decimal_to_binary::decimal_to_binary; pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal; +pub use self::decimal_to_octal::decimal_to_octal; pub use self::hexadecimal_to_binary::hexadecimal_to_binary; pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal; +pub use self::hexadecimal_to_octal::hexadecimal_to_octal; pub use self::length_conversion::length_conversion; pub use self::octal_to_binary::octal_to_binary; pub use self::octal_to_decimal::octal_to_decimal; +pub use self::octal_to_hexadecimal::octal_to_hexadecimal; pub use self::rgb_cmyk_conversion::rgb_to_cmyk; diff --git a/src/conversions/octal_to_hexadecimal.rs b/src/conversions/octal_to_hexadecimal.rs new file mode 100644 index 00000000000..fd6b3da4c87 --- /dev/null +++ b/src/conversions/octal_to_hexadecimal.rs @@ -0,0 +1,50 @@ +// Author: NithinU2802 +// Octal to Hexadecimal Converter: Converts Octal to Hexadecimal +// Wikipedia References: +// 1. https://en.wikipedia.org/wiki/Octal +// 2. https://en.wikipedia.org/wiki/Hexadecimal + +pub fn octal_to_hexadecimal(octal_str: &str) -> Result { + let octal_str = octal_str.trim(); + + if octal_str.is_empty() { + return Err("Empty string"); + } + + // Validate octal string + if !octal_str.chars().all(|c| ('0'..='7').contains(&c)) { + return Err("Invalid octal string"); + } + + // Convert octal to decimal first + let decimal = u64::from_str_radix(octal_str, 8).map_err(|_| "Conversion error")?; + + // Special case for zero + if decimal == 0 { + return Ok("0".to_string()); + } + + // Convert decimal to hexadecimal + Ok(format!("{:X}", decimal)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_octal_to_hexadecimal() { + assert_eq!(octal_to_hexadecimal("12"), Ok("A".to_string())); + assert_eq!(octal_to_hexadecimal("377"), Ok("FF".to_string())); + assert_eq!(octal_to_hexadecimal("144"), Ok("64".to_string())); + assert_eq!(octal_to_hexadecimal("0"), Ok("0".to_string())); + } + + #[test] + fn test_invalid_input() { + assert_eq!(octal_to_hexadecimal(""), Err("Empty string")); + assert_eq!(octal_to_hexadecimal("8"), Err("Invalid octal string")); + assert_eq!(octal_to_hexadecimal("9"), Err("Invalid octal string")); + assert_eq!(octal_to_hexadecimal("ABC"), Err("Invalid octal string")); + } +} From 03cc250b4cc2847ad3546bc66f5d137325d2793b Mon Sep 17 00:00:00 2001 From: NithinU2802 Date: Fri, 24 Oct 2025 22:45:30 +0530 Subject: [PATCH 2/2] fix: clippy warnings for hexadecimal and octal conversions vice versa --- src/conversions/hexadecimal_to_octal.rs | 2 +- src/conversions/octal_to_hexadecimal.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/conversions/hexadecimal_to_octal.rs b/src/conversions/hexadecimal_to_octal.rs index 2c60530248b..ed408dd0987 100644 --- a/src/conversions/hexadecimal_to_octal.rs +++ b/src/conversions/hexadecimal_to_octal.rs @@ -12,7 +12,7 @@ pub fn hexadecimal_to_octal(hex_str: &str) -> Result { } // Validate hexadecimal string - if !hex_str.chars().all(|c| c.is_digit(16)) { + if !hex_str.chars().all(|c| c.is_ascii_hexdigit()) { return Err("Invalid hexadecimal string"); } diff --git a/src/conversions/octal_to_hexadecimal.rs b/src/conversions/octal_to_hexadecimal.rs index fd6b3da4c87..933fa202334 100644 --- a/src/conversions/octal_to_hexadecimal.rs +++ b/src/conversions/octal_to_hexadecimal.rs @@ -25,7 +25,7 @@ pub fn octal_to_hexadecimal(octal_str: &str) -> Result { } // Convert decimal to hexadecimal - Ok(format!("{:X}", decimal)) + Ok(format!("{decimal:X}")) } #[cfg(test)]