Friday, 21 December 2018

Convert the string to mac address & hh format specifier


  • 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


ModifierModifiesApplies to
hhd, i, o, u, x, or Xchar, signed char or unsigned char
hd, i, o, u, x, or Xshort int or unsigned short int



Format specifierDescriptionSupported data types
%cCharacterchar
unsigned char
%nPrints nothing
%%Prints % character
 specifiers
lengthd iu o x Xf F e E g G a Acspn
(none)intunsigned intdoubleintchar*void*int*
hhsigned charunsigned char    signed char*
hshort intunsigned short int    short int*
llong intunsigned long int wint_twchar_t* long int*
lllong long intunsigned long long int    long long int*
jintmax_tuintmax_t    intmax_t*
zsize_tsize_t    size_t*
tptrdiff_tptrdiff_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.out
  Converted mac is <MAC:00:0c:29:44:00:d3>
  Converted mac is <MAC:00:0c:29:44:02:d4>

No comments:

Post a Comment