1pub struct HexUtil;
3
4impl HexUtil {
5 pub fn to_hex(bytes: &[u8]) -> String {
13 let mut hex_str = String::new();
14 for byte in bytes {
16 hex_str += &format!("{:02x}", byte);
17 }
18 hex_str
19 }
20
21 pub fn encode_hex_str(text: &str) -> String {
29 let bytes = text.as_bytes();
30 let hex_str = HexUtil::to_hex(bytes);
31 hex_str
32 }
33
34 pub fn decode_hex_str(hex: &str) -> Result<String, &'static str> {
51 if hex.len() % 2 != 0 {
53 return Err("Invalid hex string length");
54 }
55 let mut bytes = Vec::with_capacity(hex.len() / 2);
56 let mut chars = hex.chars();
57 while let (Some(a), Some(b)) = (chars.next(), chars.next()) {
59 let byte = match u8::from_str_radix(&format!("{}{}", a, b), 16) {
60 Ok(byte) => byte,
61 Err(_) => return Err("Invalid hex digit"),
62 };
63 bytes.push(byte);
64 }
65 match str::from_utf8(&bytes) {
67 Ok(s) => Ok(s.to_string()),
68 Err(_) => Err("Invalid UTF - 8 sequence"),
69 }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75
76 use super::*;
77
78 #[test]
79 fn test_to_hex() {
80 let hex_str = HexUtil::to_hex(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
81 println!("{}", hex_str);
82
83 let bytes: Vec<u8> = "hello, rust!".bytes().collect();
84 let hex_str = HexUtil::to_hex(&bytes);
85 println!("{}", hex_str);
86
87 let bytes = "hello, rust!".to_string().as_bytes().to_vec();
88 let hex_str = HexUtil::to_hex(&bytes);
89 println!("{}", hex_str);
90
91 let str = String::from("hello, rust!");
92 let bytes = str.as_bytes();
93 let hex_str = HexUtil::to_hex(bytes);
94 println!("{}", hex_str);
95 }
96
97 #[test]
98 fn test_encode_decode_hex_str() {
99 let origin = "hello, rust!";
100
101 let hex_str = HexUtil::encode_hex_str(origin);
102 println!("{}", hex_str);
103 let decoded = HexUtil::decode_hex_str(&hex_str).unwrap();
104 println!("{}", decoded);
105 assert_eq!(origin, decoded);
106 }
107}