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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#[inline]
fn utf8_byte_to_hex_position( byte: u8 ) -> u8 {
match byte {
e @ 48...57 => e - 48,
65 | 97 => 10,
66 | 98 => 11,
67 | 99 => 12,
68 | 100 => 13,
69 | 101 => 14,
70 | 102 => 15,
_ => panic!("utf8_byte_to_hex_position called with a non-ASCII Hex character. Input must only contain ASCII('a' - 'f' | 'A' - 'F' | '0' - '9')")
}
}
#[test]
fn test_utf8_byte_to_hex_position() {
assert_eq!(utf8_byte_to_hex_position('0' as u8), 0);
assert_eq!(utf8_byte_to_hex_position('1' as u8), 1);
assert_eq!(utf8_byte_to_hex_position('2' as u8), 2);
assert_eq!(utf8_byte_to_hex_position('3' as u8), 3);
assert_eq!(utf8_byte_to_hex_position('4' as u8), 4);
assert_eq!(utf8_byte_to_hex_position('5' as u8), 5);
assert_eq!(utf8_byte_to_hex_position('6' as u8), 6);
assert_eq!(utf8_byte_to_hex_position('7' as u8), 7);
assert_eq!(utf8_byte_to_hex_position('8' as u8), 8);
assert_eq!(utf8_byte_to_hex_position('9' as u8), 9);
assert_eq!(utf8_byte_to_hex_position('a' as u8), 10);
assert_eq!(utf8_byte_to_hex_position('b' as u8), 11);
assert_eq!(utf8_byte_to_hex_position('c' as u8), 12);
assert_eq!(utf8_byte_to_hex_position('d' as u8), 13);
assert_eq!(utf8_byte_to_hex_position('e' as u8), 14);
assert_eq!(utf8_byte_to_hex_position('f' as u8), 15);
assert_eq!(utf8_byte_to_hex_position('A' as u8), 10);
assert_eq!(utf8_byte_to_hex_position('B' as u8), 11);
assert_eq!(utf8_byte_to_hex_position('C' as u8), 12);
assert_eq!(utf8_byte_to_hex_position('D' as u8), 13);
assert_eq!(utf8_byte_to_hex_position('E' as u8), 14);
assert_eq!(utf8_byte_to_hex_position('F' as u8), 15);
}
#[inline]
fn hex_position_to_capital_utf8_byte( hex_position: u8 ) -> u8 {
match hex_position {
e @ 0...9 => e + 48,
10 => 65,
11 => 66,
12 => 67,
13 => 68,
14 => 69,
15 => 70,
_ => panic!("Expected an input of 0-15 in hex_position_to_capital_utf8_byte. You entered {}.", hex_position),
}
}
#[test]
fn test_hex_position_to_capital_utf8_byte() {
assert_eq!(hex_position_to_capital_utf8_byte(0), '0' as u8);
}
#[inline]
fn hex_position_to_lower_utf8_byte( hex_position: u8 ) -> u8 {
match hex_position {
e @ 0...9 => e + 48,
10 => 97,
11 => 98,
12 => 99,
13 => 100,
14 => 101,
15 => 102,
_ => panic!("Expected an input of 0-15 in hex_position_to_lower_utf8_byte. You entered {}.", hex_position),
}
}
#[test]
fn test_hex_position_to_lower_utf8_byte() {
assert_eq!(hex_position_to_lower_utf8_byte(0), '0' as u8);
}
pub fn dhex( hex_string: &str ) -> Box<Vec<u8>> {
let hex_string_bytes: Vec<u8> = hex_string.bytes().collect();
let num_of_bytes = hex_string_bytes.len() / 2;
let mut dhex: Box<Vec<u8>> = Box::new(Vec::with_capacity(num_of_bytes));
for iteration in 0..num_of_bytes {
let base_index = iteration * 2;
let new_val: u8 = (16 * utf8_byte_to_hex_position(hex_string_bytes[base_index])) + (utf8_byte_to_hex_position(hex_string_bytes[base_index + 1]));
(*dhex).push(new_val);
}
dhex
}
#[test]
fn test_dhex() {
let hex_string = "0F12FF00a7";
let hex_vals = *dhex(hex_string);
assert_eq!(hex_vals[0], 15);
assert_eq!(hex_vals[1], 18);
assert_eq!(hex_vals[2], 255);
assert_eq!(hex_vals[3], 0);
assert_eq!(hex_vals[4], 167);
}
pub fn capital_hex( hex_values: &Vec<u8> ) -> Box<String> {
let num_of_bytes = hex_values.len();
let mut hex_string: Box<String> = Box::new(String::with_capacity(num_of_bytes));
for index in 0..num_of_bytes {
let byte: u8 = hex_values[index];
let two_position: u8 = byte / 16;
let one_position: u8 = byte - (two_position * 16);
let left_char: u8 = hex_position_to_capital_utf8_byte(two_position);
hex_string.push(left_char as char);
let right_char: u8 = hex_position_to_capital_utf8_byte(one_position);
hex_string.push(right_char as char);
}
hex_string
}
#[test]
fn test_capital_hex() {
let hex_vals: Vec<u8> = vec![15,18,255,0,167];
let hex_string = *capital_hex(&hex_vals);
assert_eq!(hex_string, "0F12FF00A7");
}
pub fn lower_hex( hex_values: &Vec<u8> ) -> Box<String> {
let num_of_bytes = hex_values.len();
let mut hex_string: Box<String> = Box::new(String::with_capacity(num_of_bytes));
for index in 0..num_of_bytes {
let byte: u8 = hex_values[index];
let two_position: u8 = byte / 16;
let one_position: u8 = byte - (two_position * 16);
let left_char: u8 = hex_position_to_lower_utf8_byte(two_position);
hex_string.push(left_char as char);
let right_char: u8 = hex_position_to_lower_utf8_byte(one_position);
hex_string.push(right_char as char);
}
hex_string
}
#[test]
fn test_lower_hex() {
let hex_vals: Vec<u8> = vec![15,18,255,0,167];
let hex_string = *lower_hex(&hex_vals);
assert_eq!(hex_string, "0f12ff00a7");
}