把mac地址转换为标准mac地址

把"00:90:8A:1D:30:51"转换成"00-90-8A-1D-30-51",如何格式错误,显示出格式错误的种类,有些不规范的转换成规范的格式,例如,"1234:8F:90-D1:76",为不规范格式转换成

标准格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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;
}
posted @   xshang  阅读(6874)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示