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

时间:2023-07-18 15:40:07

把"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;
}