How do you validate that a string is a valid MAC address in C?

前端 未结 4 584
挽巷
挽巷 2020-12-22 14:43

example:

12:45:ff:ab:aa:cd    is valid
45:jj:jj:kk:ui>cd    is not valid
4条回答
  •  攒了一身酷
    2020-12-22 15:17

    The following code checks for valid MAC addresses (with or w/o the ":" separator):

    #include 
    
    int isValidMacAddress(const char* mac) {
        int i = 0;
        int s = 0;
    
        while (*mac) {
           if (isxdigit(*mac)) {
              i++;
           }
           else if (*mac == ':' || *mac == '-') {
    
              if (i == 0 || i / 2 - 1 != s)
                break;
    
              ++s;
           }
           else {
               s = -1;
           }
    
    
           ++mac;
        }
    
        return (i == 12 && (s == 5 || s == 0));
    }
    

    The code checks the following:

    • that the input string mac contains exactly 12 hexadecimal digits.
    • that, if a separator colon : appears in the input string, it only appears after an even number of hex digits.

    It works like this:

    • i,which is the number of hex digits in mac, is initialized to 0.
    • The while loops over every character in the input string until either the string ends, or 12 hex digits have been detected.
      • If the current character (*mac) is a valid hex digit, then i is incremented, and the loop examines the next character.
      • Otherwise, the loop checks if the current character is a separator (colon or a dash); if so, it verifies that there is one separator for every pair of hex digits. Otherwise, the loop is aborted.
    • After the loop finishes, the function checks if 12 hex digits have been found, and zero or exactly five separators, and returns the result.

    If you don't want to accept separators, simply change the return statement to:

    return (i == 12 && s == 0);
    

提交回复
热议问题