Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bin_2_hex and hex_2_bin #80

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
28 changes: 28 additions & 0 deletions rust/src/string/bin_2_hex.rs
Original file line number Diff line number Diff line change
@@ -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<String, Bin2HexError> {
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);
}
31 changes: 31 additions & 0 deletions rust/src/string/hex_2_bin.rs
Original file line number Diff line number Diff line change
@@ -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<String, Hex2BinError> {
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)
}
}
}
10 changes: 10 additions & 0 deletions rust/tests/string_test/bin_2_hex_test.rs
Original file line number Diff line number Diff line change
@@ -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")));
}
}
10 changes: 10 additions & 0 deletions rust/tests/string_test/hex_2_bin_test.rs
Original file line number Diff line number Diff line change
@@ -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!@#")));
}
}