-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathhexadecimal_to_octal.rs
More file actions
64 lines (53 loc) · 1.75 KB
/
hexadecimal_to_octal.rs
File metadata and controls
64 lines (53 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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")
);
}
}