From 07839aa7a3e69fdbf8175b923fed34e90c898c17 Mon Sep 17 00:00:00 2001 From: Iiqbal2000 Date: Sun, 8 Oct 2023 16:42:16 +0700 Subject: [PATCH] feat: add bin_2_hex and hex_2_bin --- rust/src/string/bin_2_hex.rs | 28 +++++++++++++++++++++ rust/src/string/hex_2_bin.rs | 31 ++++++++++++++++++++++++ rust/tests/string_test/bin_2_hex_test.rs | 10 ++++++++ rust/tests/string_test/hex_2_bin_test.rs | 10 ++++++++ 4 files changed, 79 insertions(+) create mode 100644 rust/src/string/bin_2_hex.rs create mode 100644 rust/src/string/hex_2_bin.rs create mode 100644 rust/tests/string_test/bin_2_hex_test.rs create mode 100644 rust/tests/string_test/hex_2_bin_test.rs diff --git a/rust/src/string/bin_2_hex.rs b/rust/src/string/bin_2_hex.rs new file mode 100644 index 0000000..0e6129c --- /dev/null +++ b/rust/src/string/bin_2_hex.rs @@ -0,0 +1,28 @@ +#[derive(Debug, PartialEq)] +pub enum Bin2HexError { + EmptyStr, + NonASCII +} + +// The bin_2_hex function converts a string of ASCII characters to their hexadecimal representation. +// Parameter: +// - s => string that is to be converted +// +// Return: +// - A string of their hexadecimal representation +pub fn bin_2_hex(s: &str) -> Result { + if s.is_empty() { + return Err(Bin2HexError::EmptyStr); + } + + let mut result: String = String::new(); + for c in s.as_bytes() { + if !c.is_ascii() { + return Err(Bin2HexError::NonASCII); + } + + result.push_str(&format!("{:x}", c)) + } + + return Ok(result); +} \ No newline at end of file diff --git a/rust/src/string/hex_2_bin.rs b/rust/src/string/hex_2_bin.rs new file mode 100644 index 0000000..4be93d8 --- /dev/null +++ b/rust/src/string/hex_2_bin.rs @@ -0,0 +1,31 @@ +#[derive(Debug, PartialEq)] +pub enum Hex2BinError { + EmptyInput, + InvalidLength, + InvalidHexCharacter(std::num::ParseIntError), +} + +const HEX_CHAR_LEN: usize = 2; +const HEX_BASE: u32 = 16; + +// The hex_2_bin function converts a string of hexadecimal to the ascii characters. +// Parameter: +// - s => hexadecimal that is to be converted +// +// Return: +// - a string of ASCII characters corresponding to the input hexadecimal +pub fn hex_2_bin(hex: &str) -> Result { + match hex.len() { + 0 => Err(Hex2BinError::EmptyInput), + _ if hex.len() % 2 != 0 => Err(Hex2BinError::InvalidLength), + _ => { + let mut result = String::new(); + for i in (0..hex.len()).step_by(HEX_CHAR_LEN) { + let byte_str = &hex[i..i + HEX_CHAR_LEN]; + let byte = u8::from_str_radix(byte_str, HEX_BASE).map_err(Hex2BinError::InvalidHexCharacter)?; + result.push(byte as char); + } + Ok(result) + } + } +} \ No newline at end of file diff --git a/rust/tests/string_test/bin_2_hex_test.rs b/rust/tests/string_test/bin_2_hex_test.rs new file mode 100644 index 0000000..4e34a0b --- /dev/null +++ b/rust/tests/string_test/bin_2_hex_test.rs @@ -0,0 +1,10 @@ +#[cfg(test)] +mod tests { + use pehape_rs::bin_2_hex; + + #[test] + fn test_bin_2_hex() { + assert_eq!(bin_2_hex("Hello World!!"), Ok(String::from("48656c6c6f20576f726c642121"))); + assert_eq!(bin_2_hex("hacktoberfest1234!@#"), Ok(String::from("6861636b746f6265726665737431323334214023"))); + } +} \ No newline at end of file diff --git a/rust/tests/string_test/hex_2_bin_test.rs b/rust/tests/string_test/hex_2_bin_test.rs new file mode 100644 index 0000000..84cb4aa --- /dev/null +++ b/rust/tests/string_test/hex_2_bin_test.rs @@ -0,0 +1,10 @@ +#[cfg(test)] +mod tests { + use pehape_rs::hex_2_bin; + + #[test] + fn test_hex_2_bin() { + assert_eq!(hex_2_bin("48656c6c6f20576f726c642121"), Ok(String::from("Hello World!!"))); + assert_eq!(hex_2_bin("6861636b746f6265726665737431323334214023"), Ok(String::from("hacktoberfest1234!@#"))); + } +} \ No newline at end of file