把mac地址转换为标准mac地址
生活随笔
收集整理的這篇文章主要介紹了
把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];}elsecontinue; }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; }轉載于:https://www.cnblogs.com/xshang/p/3370474.html
總結
以上是生活随笔為你收集整理的把mac地址转换为标准mac地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络防火墙实战-基于pfsense(1)
- 下一篇: mfc创建一个word操作工程 【转自: