Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions src/conversions/binary_to_octal.rs
Original file line number Diff line number Diff line change
@@ -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<String, &'static str> {
// 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::<Vec<char>>().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"));
}
}
37 changes: 37 additions & 0 deletions src/conversions/decimal_to_octal.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
64 changes: 64 additions & 0 deletions src/conversions/hexadecimal_to_octal.rs
Original file line number Diff line number Diff line change
@@ -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<String, &'static str> {
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_ascii_hexdigit()) {
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")
);
}
}
8 changes: 8 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
50 changes: 50 additions & 0 deletions src/conversions/octal_to_hexadecimal.rs
Original file line number Diff line number Diff line change
@@ -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<String, &'static str> {
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!("{decimal:X}"))
}

#[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"));
}
}