1pub struct HexUtil;
3
4impl HexUtil {
5
6
7 pub fn to_hex(bytes: &[u8]) -> String {
8 let mut hex_str = String::new();
9 for byte in bytes {
10 hex_str += &format!("{:02x}", byte);
11 }
12 hex_str
13 }
14
15}
16
17
18#[cfg(test)]
19mod tests {
20
21 use super::*;
22
23 #[test]
24 fn test_to_hex() {
25 let hex_str = HexUtil::to_hex(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
26 println!("{}", hex_str);
27
28 let bytes: Vec<u8> = "hello, rust!".bytes().collect();
29 let hex_str = HexUtil::to_hex(&bytes);
30 println!("{}", hex_str);
31
32 let bytes = "hello, rust!".to_string().as_bytes().to_vec();
33 let hex_str = HexUtil::to_hex(&bytes);
34 println!("{}", hex_str);
35
36 let str = String::from("hello, rust!");
37 let bytes = str.as_bytes();
38 let hex_str = HexUtil::to_hex(bytes);
39 println!("{}", hex_str);
40 }
41
42}