-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from Iiqbal2000/feat/bin2hex
feat: add bin_2_hex and hex_2_bin
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!@#"))); | ||
} | ||
} |