- Use hh format specifier to convert the string into a MAC address.
int sscanf(const char *str, const char *format, ...)
Description:
hh Specifies that a following d, i, o, u, x, or X
conversion specifier applies to a signed char
or unsigned char argument (the argument will
have been promoted according to the integer
promotions, but its value shall be converted to
signed char or unsigned char before printing);
or that a following n conversion specifier
applies to a pointer to a signed char argument.
Table
Modifier | Modifies | Applies to |
---|---|---|
hh | d, i, o, u, x, or X | char , signed char or unsigned char |
h | d, i, o, u, x, or X | short int or unsigned short int |
Format specifier | Description | Supported data types |
---|---|---|
%c | Character | char unsigned char |
%n | Prints nothing | |
%% | Prints % character |
specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x X | f F e E g G a A | c | s | p | n |
(none) | int | unsigned int | double | int | char* | void* | int* |
hh | signed char | unsigned char | signed char* | ||||
h | short int | unsigned short int | short int* | ||||
l | long int | unsigned long int | wint_t | wchar_t* | long int* | ||
ll | long long int | unsigned long long int | long long int* | ||||
j | intmax_t | uintmax_t | intmax_t* | ||||
z | size_t | size_t | size_t* | ||||
t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
L | long double |
Program:
/* Convert the string to mac address & hh format specifier by Velraj.K
* Check : http://velrajcoding.blogspot.in
*/
#include <stdio.h>
#include <stdint.h>
#define MAC_LEN 6
#define MAC_STR_BUF_LEN 32
#define MAC_INDEX_0 0
#define MAC_INDEX_1 1
#define MAC_INDEX_2 2
#define MAC_INDEX_3 3
#define MAC_INDEX_4 4
#define MAC_INDEX_5 5
typedef uint8_t tMacAddr[MAC_LEN];
int convert_string_to_mac_address(const char *str_mac, tMacAddr mac_addr, int pattern)
{
int ret = 0;
char *format = NULL;
char str_mac_buf[MAC_STR_BUF_LEN] = {'\0'};
switch (pattern) {
case 0:
format = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
break;
case 1:
format = "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx";
break;
default:
printf("Pattern for MAC is invalid <%d> \n", pattern);
return 0;
break;
}
/* Convert the mac MAC address from string */
ret = sscanf(str_mac, format, &mac_addr[MAC_INDEX_0], &mac_addr[MAC_INDEX_1], &mac_addr[MAC_INDEX_2],
&mac_addr[MAC_INDEX_3], &mac_addr[MAC_INDEX_4], &mac_addr[MAC_INDEX_5]);
if (ret != MAC_LEN) {
snprintf(str_mac_buf, sizeof(str_mac_buf), format, mac_addr[MAC_INDEX_0], mac_addr[MAC_INDEX_1],
mac_addr[MAC_INDEX_2], mac_addr[MAC_INDEX_3], mac_addr[MAC_INDEX_4], mac_addr[MAC_INDEX_5]);
printf("Error in converting the mac from string %s ret <%d>", str_mac_buf, ret);
return 0;
}
return 0;
}
int main()
{
tMacAddr mac1 = {0}, mac_no_colon = {0};
convert_string_to_mac_address("00:0c:29:44:00:d3", mac1, 0);
printf("Converted mac is <MAC:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx> \n", mac1[MAC_INDEX_0], mac1[MAC_INDEX_1], mac1[MAC_INDEX_2],
mac1[MAC_INDEX_3], mac1[MAC_INDEX_4], mac1[MAC_INDEX_5]);
convert_string_to_mac_address("000c294402d4", mac_no_colon, 1);
printf("Converted mac is <MAC:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx> \n", mac_no_colon[MAC_INDEX_0], mac_no_colon[MAC_INDEX_1], mac_no_colon[MAC_INDEX_2],
mac_no_colon[MAC_INDEX_3], mac_no_colon[MAC_INDEX_4], mac_no_colon[MAC_INDEX_5]);
return 0;
}
Output:
$/sample$ ./a.outConverted mac is <MAC:00:0c:29:44:00:d3>
Converted mac is <MAC:00:0c:29:44:02:d4>