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>

Sizeof format zu or u

Sizeof format zu or u



  • It is a compile time unary operator which can be used to compute the size of its operand.
  • The result of sizeof is of unsigned integral type which is usually denoted by size_t.
  • sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.
  • It simply return amount of memory is allocated to that data types.

 

Program:


/* sizeof by Velraj.K
   Check : http://velrajcoding.blogspot.in
 */

#include <stdio.h>
 

#define STRNAME "Velraj"

int main()
{
        printf("Vel size = %zu int= %zu float = %zu double= %zu 0 = %zu NULL = %zu  \"\" = %zu int * = %zu\n",
                sizeof("vel"), sizeof(int), sizeof(float), sizeof(double), sizeof(0), sizeof(NULL), sizeof(""), sizeof(int *));

        return 0;
}

Output:

   velrajk/sample$ ./a.out
   Vel size = 4 int= 4 float = 4 double= 8 0 = 4 NULL = 8  "" = 1 int * = 8