Skip to content

Commit 83702a8

Browse files
authored
Add Hexadecimal To Decimal Conversion (TheAlgorithms#657)
1 parent f3dda8d commit 83702a8

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs)
4545
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_hexadecimal.rs)
4646
* [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs)
47+
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs)
4748
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
4849
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
4950
* Data Structures
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
pub fn hexadecimal_to_decimal(hexadecimal_str: &str) -> Result<u64, &'static str> {
2+
if hexadecimal_str.is_empty() {
3+
return Err("Empty input");
4+
}
5+
6+
for hexadecimal_str in hexadecimal_str.chars() {
7+
if !hexadecimal_str.is_ascii_hexdigit() {
8+
return Err("Input was not a hexadecimal number");
9+
}
10+
}
11+
12+
match u64::from_str_radix(hexadecimal_str, 16) {
13+
Ok(decimal) => Ok(decimal),
14+
Err(_e) => Err("Failed to convert octal to hexadecimal"),
15+
}
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::*;
21+
22+
#[test]
23+
fn test_hexadecimal_to_decimal_empty() {
24+
assert_eq!(hexadecimal_to_decimal(""), Err("Empty input"));
25+
}
26+
27+
#[test]
28+
fn test_hexadecimal_to_decimal_invalid() {
29+
assert_eq!(
30+
hexadecimal_to_decimal("xyz"),
31+
Err("Input was not a hexadecimal number")
32+
);
33+
assert_eq!(
34+
hexadecimal_to_decimal("0xabc"),
35+
Err("Input was not a hexadecimal number")
36+
);
37+
}
38+
39+
#[test]
40+
fn test_hexadecimal_to_decimal_valid1() {
41+
assert_eq!(hexadecimal_to_decimal("45"), Ok(69));
42+
assert_eq!(hexadecimal_to_decimal("2b3"), Ok(691));
43+
assert_eq!(hexadecimal_to_decimal("4d2"), Ok(1234));
44+
assert_eq!(hexadecimal_to_decimal("1267a"), Ok(75386));
45+
}
46+
47+
#[test]
48+
fn test_hexadecimal_to_decimal_valid2() {
49+
assert_eq!(hexadecimal_to_decimal("1a"), Ok(26));
50+
assert_eq!(hexadecimal_to_decimal("ff"), Ok(255));
51+
assert_eq!(hexadecimal_to_decimal("a1b"), Ok(2587));
52+
assert_eq!(hexadecimal_to_decimal("7fffffff"), Ok(2147483647));
53+
}
54+
55+
#[test]
56+
fn test_hexadecimal_to_decimal_valid3() {
57+
assert_eq!(hexadecimal_to_decimal("0"), Ok(0));
58+
assert_eq!(hexadecimal_to_decimal("7f"), Ok(127));
59+
assert_eq!(hexadecimal_to_decimal("80000000"), Ok(2147483648));
60+
}
61+
}

src/conversions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ mod binary_to_hexadecimal;
33
mod decimal_to_binary;
44
mod decimal_to_hexadecimal;
55
mod hexadecimal_to_binary;
6+
mod hexadecimal_to_decimal;
67
mod octal_to_binary;
78
mod octal_to_decimal;
89
pub use self::binary_to_decimal::binary_to_decimal;
910
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
1011
pub use self::decimal_to_binary::decimal_to_binary;
1112
pub use self::decimal_to_hexadecimal::decimal_to_hexadecimal;
1213
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
14+
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
1315
pub use self::octal_to_binary::octal_to_binary;
1416
pub use self::octal_to_decimal::octal_to_decimal;

src/sorting/bingo_sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn max_min(vec: &[i32], bingo: &mut i32, next_bingo: &mut i32) {
88
}
99
}
1010

11-
pub fn bingo_sort(vec: &mut Vec<i32>) {
11+
pub fn bingo_sort(vec: &mut [i32]) {
1212
if vec.is_empty() {
1313
return;
1414
}

0 commit comments

Comments
 (0)