1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/// Converts a byte slice to its hexadecimal string representation.
///
/// This function takes a slice of bytes and returns a String containing the
/// hexadecimal representation of those bytes. Each byte is converted to a
/// two-character hexadecimal string.
///
/// # Arguments
///
/// * `bytes` - A slice of bytes to convert to hexadecimal.
///
/// # Returns
///
/// A String containing the hexadecimal representation of the input bytes.
///
/// # Example
/// ```rust
/// let bytes = &[15, 255, 0, 128];
/// let hex_string = byteutils::bytes_to_hex(bytes);
/// assert_eq!(byteutils::bytes_to_hex(bytes), "0fff0080");
/// ````
///
/// Converts a hexadecimal string to its byte representation.
///
/// This function takes a string slice containing a hexadecimal representation
/// and returns a Vec<u8> containing the corresponding bytes. The input string
/// must have an even number of characters and contain only valid hexadecimal
/// digits (0-9, a-f, A-F).
///
/// # Arguments
///
/// * `hex` - A string slice containing the hexadecimal representation to convert.
///
/// # Returns
///
/// A Result containing either:
/// - Ok(Vec<u8>): The byte representation of the input hexadecimal string.
/// - Err(String): An error message if the input is invalid.
///
/// # Example
/// ```rust
/// let bytes = byteutils::hex_to_bytes("0fff0080").unwrap();
/// assert_eq!(byteutils::hex_to_bytes("0fff0080").unwrap(), vec![15, 255, 0, 128]);
/// ````
///
/// Converts a byte slice to a UTF-8 string.
///
/// This function takes a slice of bytes and attempts to convert it to a UTF-8 string.
/// If the bytes do not represent valid UTF-8 data, an error is returned.
///
/// # Arguments
///
/// * `bytes` - A slice of bytes to convert to a string.
///
/// # Returns
///
/// A Result containing either:
/// - Ok(String): The UTF-8 string representation of the input bytes.
/// - Err(String): An error message if the bytes are not valid UTF-8.
///
/// # Example
/// ```rust
/// let bytes = "Hello".as_bytes();
/// let string = byteutils::bytes_to_string(bytes).unwrap();
/// assert_eq!(string, "Hello");
/// ```
///
/// Converts a string to its byte representation.
///
/// This function takes a string slice and returns a vector of bytes
/// containing its UTF-8 representation.
///
/// # Arguments
///
/// * `s` - A string slice to convert to bytes.
///
/// # Returns
///
/// A Vec<u8> containing the UTF-8 byte representation of the input string.
///
/// # Example
/// ```rust
/// let string = "Hello";
/// let bytes = byteutils::string_to_bytes(string);
/// assert_eq!(bytes, vec![72, 101, 108, 108, 111]);
/// ```
///
/// Converts a string to its hexadecimal representation.
///
/// This function takes a string slice and returns a hexadecimal string
/// representing the UTF-8 bytes of the input string.
///
/// # Arguments
///
/// * `s` - A string slice to convert to hexadecimal.
///
/// # Returns
///
/// A String containing the hexadecimal representation of the input string's UTF-8 bytes.
///
/// # Example
/// ```rust
/// let string = "Hello";
/// let hex = byteutils::string_to_hex(string);
/// assert_eq!(hex, "48656c6c6f");
/// ```
///
/// Converts a hexadecimal string to a UTF-8 string.
///
/// This function takes a hexadecimal string and attempts to convert it to a UTF-8 string.
/// The input hex string must have an even number of characters and contain only valid
/// hexadecimal digits. The resulting bytes must form a valid UTF-8 sequence.
///
/// # Arguments
///
/// * `hex` - A string slice containing the hexadecimal representation to convert.
///
/// # Returns
///
/// A Result containing either:
/// - Ok(String): The UTF-8 string representation of the input hexadecimal.
/// - Err(String): An error message if the input is invalid or the bytes are not valid UTF-8.
///
/// # Example
/// ```rust
/// let hex = "48656c6c6f";
/// let string = byteutils::hex_to_string(hex).unwrap();
/// assert_eq!(string, "Hello");
/// ```
///