把mac地址轉換為標准mac地址


把"00:90:8A:1D:30:51"轉換成"00-90-8A-1D-30-51",如何格式錯誤,顯示出格式錯誤的種類,有些不規范的轉換成規范的格式,例如,"1234:8F:90-D1:76",為不規范格式轉換成

標准格式。

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

void getStandardMac(char str[],char s[])
{
    int count=0;
    int temp=0;
    int pre=1;
    char c;
    for(int i=0;i<strlen(str);i++) //判斷字符數是否為6字節
       {
           if(str[i]>='0'&&str[i]<='9'||str[i]>='A'&&str[i]<='F')
              {
               count++;   
              }
       }
     if(count!=12)
        {
           cout<<"mac is not 6 字節"<<endl;
           exit(0);
        }
      for(int j=0;j<strlen(str);j++)
         {
           if((pre-temp)%2!=0||(pre-temp)==0)         //判斷分隔符之間的字符是否為偶數
             {
                temp=pre;
           if(str[j]==':'||str[j]=='-')
              {
                if(j>2&&(j-pre)==1)
                  {
                     cout<<"出現了兩個連續字符."<<endl;
                     exit(0);
                  }
                pre=j;
              }
             }
             else
             {
                 cout<<"分隔符之間的字符數不為偶數"<<endl;
                 exit(0);
             }
         }
         temp=0;
       for(int k=0;k<strlen(str);k++)
          {
             
                        
                 if(temp==2||temp==5||temp==8||temp==11||temp==14)
                     {
                        s[temp++]='-';
                        k--;
                     }
                  else if(str[k]>='0'&&str[k]<='9'||str[k]>='A'&&str[k]<='F')
                     {
                     s[temp++]=str[k];
                     }
                   else
                       continue;  
                   
          }
        s[temp]='\0';
}
int main()
{
   char str[256];
   char s[256];
   cin>>str;
   cout<<"原mac地址為:"<<str<<endl;
   getStandardMac(str,s);
   cout<<"mac標准地址為:"<<endl;
   cout<<s<<endl;
   return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM