0

例:

12:45:ff:ab:aa:cd は有効です
45:jj:jj:kk:ui>cd は無効です
4

4 に答える 4

11

次のコードは、有効な MAC アドレスをチェックします (「:」区切り記号の有無にかかわらず)。

#include <ctype.h>

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));
}

コードは以下をチェックします。

  • 入力文字列macに正確に 12 桁の 16 進数が含まれていること。
  • つまり、区切りコロン:が入力文字列に表示される場合、偶数の 16 進数の後にのみ表示されます。

それはこのように動作します:

  • iの 16 進数の数である は、mac0 に初期化されます。
  • while、文字列が終了するか、12 桁の 16 進数が検出されるまで、入力文字列のすべての文字をループします。
    • 現在の文字 ( *mac) が有効な 16 進数である場合、iはインクリメントされ、ループは次の文字を調べます。
    • それ以外の場合、ループは現在の文字が区切り文字 (コロンまたはダッシュ) であるかどうかをチェックします。その場合、16 進数のペアごとに 1 つのセパレーターがあることを確認します。それ以外の場合、ループは中止されます。
  • ループが終了すると、関数は 12 桁の 16 進数が見つかったかどうか、および区切り文字が 0 個かちょうど 5 個かどうかをチェックし、結果を返します。

セパレーターを受け入れたくない場合は、単に return ステートメントを次のように変更します。

return (i == 12 && s == 0);
于 2011-01-25T10:13:56.330 に答える
0

ANSI C で MAC アドレスの検証と解析の両方が必要だったので、ここに関数を示します。MAC アドレスが検証された場合は 1 を返します (12 個の 16 進文字、小文字または大文字、コロンの有無にかかわらず、 のような部分的に正しい入力を含む) を受け入れますb3:0a:23:48fad3)。Cortex m3コントローラー上の組み込みアプリケーションで、私のために仕事をしてくれます。
この関数は、Web ページからの直接入力も受け入れるため (これが実際の使用方法です)、コロンを%3A文字として受け入れます。
結果は 6 バイト配列です。あなたは#include <cctype>これをしなければならないでしょう。
関数を機能させるには、入力文字列 ( *mac) をゼロで終了する必要があります。

int parse_mac_address(char* mac, unsigned char *mac_address) {
int index=0;
char temp_mac[12]={0,0,0,0,0,0,0,0,0,0,0,0};

memset(mac_address, 0, 6);              // Clear the 6 needed bytes in target array (which should be defined as "unsigned char mac_address[6]")

while(*mac) {                           // Repeat while we have something in the string
    if(index>11) {                      // The string may be longer than our 12 digits
        return 0;                       // If it has too many digits, break out and return an error (zero)
    }

    if(isxdigit(*mac)) {                // If this is a hex digit
        if(isdigit(*mac)) {             // If the digit is 0 to 9
            temp_mac[index]=*mac-0x30;  // Convert to a number
        }
        else {                          // If the digit is A to F
            temp_mac[index]=toupper(*mac)-0x37; // Make sure it is an upper case letter and convert to a number
        }
        ++mac;                          // Go further on the mac string
        ++index;                        // Promote the index to the next value in our array
    }
    else {
        if(!(index%2) && *mac==':') {   // If it was not a digit, it can be a colon, but only on an odd locations in the array
            ++mac;
        }
        else {
            if(!(index%2) && *mac=='%' && *(mac+1)=='3' && toupper(*(mac+2))=='A') { // In case of web colon sign we receive '%3A' instead, so we have to jump 3 characters to the next digit
                mac+=3; 
            }
            else {                      // If we discovered anything else, this is not a valid mac address - break out and return an error (zero)
                return 0;
            }
        }
    }
}

if(index!=11) {                         // Last check - the index must be exactly 11, to indicate we have filled in 12 digits
    return 0;                           // If not - return with error (zero)
}

for(index=0;index<6;index++) {          // Now, when we have 12 digits in an array, we will convert them in to two-digit bytes
    *(mac_address+5-index)=temp_mac[index*2+1]+temp_mac[index*2]*0x10; // Taking pairs of digits and converting them in to a byte
    // This operation will make mac_address[0] the LSB and mac_address[5] the MSB
    // If you need it the other way round, replace *(mac_address+5-index) with *(mac_address+index)
}

return 1;                               // Return OK (one)
}
于 2014-02-24T08:38:40.627 に答える
0

MAC アドレスの健全性をチェックする別の簡単な関数を次に示します。

#include <stdio.h>
#include <string.h>

typedef unsigned char byte;

#define ETH_ADDR_LEN    6
#define FALSE           0
#define TRUE            1
#define MAC_STR_LEN     18
#define SEPERATOR       ':'

int isMacValid(char *MacAddress) {
    char mac_part[3] = {0};
    byte mac_byte    = 0;
    byte mac_idx     = 0;

    while(MacAddress[mac_idx] != 0 && mac_idx < MAC_STR_LEN){
        if(mac_idx != 15 && MacAddress[mac_idx + 2] != SEPERATOR)
            return FALSE;
        strncpy(mac_part, MacAddress+mac_idx,2);
        mac_byte = (byte)strtol(mac_part, NULL, 16);
        if(mac_byte == 0 && strcmp(mac_part,"00"))
            return FALSE;
        mac_idx += 3;
    }

    if(mac_idx == MAC_STR_LEN)
        return TRUE;
    return FALSE;
}
于 2017-05-18T16:32:16.713 に答える