1#![allow(dead_code)]
2pub mod ascii_to_hex {
10
11 pub fn get_hex_string<'a>(ascii_string: &'a str) -> String {
13 let ascii_bytes = get_bytes(ascii_string);
14 let hex_vec = convert_to_hex(ascii_bytes);
15 concatenate_slices(hex_vec)
16 }
17
18 fn get_bytes<'a>(ascii_string: &'a str) -> &'a [u8] {
22 ascii_string.as_bytes()
23 }
24 fn convert_to_hex<'a>(ascii_bytes: &'a [u8]) -> Vec<String> {
28 ascii_bytes
29 .into_iter()
30 .map(|byte| format!("{:X}", byte).as_str().to_owned())
31 .collect()
32 }
33 fn concatenate_slices<'a>(hex_vec: Vec<String>) -> String {
35 hex_vec.concat()
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use super::ascii_to_hex;
42
43 #[test]
44 fn it_works() {
45 let test_string = "test";
46 assert_eq!(ascii_to_hex::get_hex_string(test_string), "74657374");
47 }
48
49 #[test]
50 fn empty_string() {
51 let test_string = "";
52 assert_eq!(ascii_to_hex::get_hex_string(test_string), "");
53 }
54
55 #[test]
56 fn long_string() {
57 let test_string = "DSJAKLFHASJKGLF879Q472631987HJKFSDAHFLKSJADHsdahlkfjhdskalfh";
58 assert_eq!(ascii_to_hex::get_hex_string(test_string), "44534A414B4C464841534A4B474C4638373951343732363331393837484A4B4653444148464C4B534A414448736461686C6B666A6864736B616C6668");
59 }
60
61 #[test]
62 fn special_characters() {
63 let test_string = "!@#%";
64 assert_eq!(ascii_to_hex::get_hex_string(test_string), "21402325");
65 }
66}