-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype_conversion.c
51 lines (44 loc) · 1.7 KB
/
datatype_conversion.c
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
#include "ft_malcolm.h"
void _mc_convert_string_to_byte_ip(const char* str_ip, uint8_t* byte_ip)
{
in_addr_t ip = inet_addr(str_ip);
uint8_t* ptr = (uint8_t*)&ip;
for (int i = 0; i < 4; i++) {
byte_ip[i] = ptr[i];
}
}
unsigned char _mc_hex_char_to_byte(char c)
{
if (c >= '0' && c <= '9') {
// Subtract the ASCII value of '0' from c to get the decimal value of the digit.
// It is then casted to an unsigned char and returned as the byte value.
return (unsigned char)(c - '0');
} else if (c >= 'a' && c <= 'f') {
// By subtracting the ASCII value of 'a' from the character c, we get the offset
// from 'a'. Adding 10 ensures that 'a' corresponds to 10, 'b' to 11, and so on.
return (unsigned char)(c - 'a' + 10);
} else if (c >= 'A' && c <= 'F') {
// Same logic but for upppercase letters
return (unsigned char)(c - 'A' + 10);
} else {
return 0; // Invalid character, return 0 as default
}
}
void _mc_convert_mac_string_to_bytes(const char* mac_string, unsigned char* mac_bytes)
{
int byte_index = 0;
int str_index = 0;
while (mac_string[str_index] && byte_index < 6) {
// Skip delimiters (':', '-')
if (mac_string[str_index] == ':' || mac_string[str_index] == '-') {
str_index++;
continue;
}
// Convert two hexadecimal characters to a byte
unsigned char high_nibble = _mc_hex_char_to_byte(mac_string[str_index]);
unsigned char low_nibble = _mc_hex_char_to_byte(mac_string[str_index + 1]);
mac_bytes[byte_index++] = (high_nibble << 4) | low_nibble;
// Move to the next pair of characters
str_index += 2;
}
}