2013年7月2日 星期二

[C][source][str2mac]

 #include <stdio.h>  
 #include <limits.h>  
 #include <string.h>  
 #include <stdint.h>  
 int str2mac(char *in, uint8_t *mac)  
 {  
     char c1, c2;  
     int len, i;  
     len = strlen(in);  
     for (i = 0; i < (len / 2); i ++) {  
         c1 = toupper(in[2*i]);  
         c2 = toupper(in[2*i + 1]);  
         if (c1 < '0' || (c1 > '9' && c1 <'A') || c1 > 'F')  
       return 0;  
         if (c2 < '0' || (c2 > '9' && c2 <'A') || c2 > 'F')  
       return 0;  
         c1 = (c1 > '9') ? (c1 - 'A' + 10) : (c1 - '0');  
         c2 = (c2 > '9') ? (c2 - 'A' + 10) : (c2 - '0');  
         mac[i] = c1 << 4 | c2;  
     }  
     return 1;  
 }  
 int main(int argc, char *argv[])  
 {  
     uint8_t mac[6];  
     int i;  
     memset(&mac, 0x0, sizeof(mac));  
     str2mac("0012cf368901", mac);  
     for (i = 0; i < 5; i ++) {  
         printf("%02X:", mac[i]);  
     }  
     printf("\n");  
     return 0;  
 }  

沒有留言:

張貼留言