一种根据MAC地址自动生成设备名称的方法(一)

2017-12-14 20:01:48来源:CSDN作者:u011852211人点击

分享
#include <stdio.h>typedef unsigned char u_int8;typedef unsigned int u_int32;void setDeviceName(u_int8 *name, u_int8 *macAddr);int main(){    u_int8 deviceName[16] = "Device_11111";    u_int8 macAddrStr[6] = {0x02,0x11,0x23,0x34,0x56,0xf0};    //当前设备的mac地址    setDeviceName(deviceName, macAddrStr);    printf("%s/n",deviceName);    return 0;}void setDeviceName(u_int8 name[], u_int8 mac[]){    u_int8 macAddrOrgStr[6] = {0x02,0x11,0x23,0x34,0x56,0xee}; //人为设定的起始mac地址    u_int32 macAddrOrg = 0; //人为设定的起始mac地址    u_int32 macAddr = 0;    //当前设备的mac地址    u_int32 deviceNameOrgSuffix = 11111; //人为设定的设备起始编号,如11111    u_int32 deviceNameSuffix = 0;  //设备名称后缀    macAddrOrg = (macAddrOrgStr[2]<<24) | (macAddrOrgStr[3]<<16) | (macAddrOrgStr[4]<<8) | macAddrOrgStr[5]; //取出mac地址的后四个字节    macAddr = (mac[2]<<24) | (mac[3]<<16) | (mac[4]<<8) | mac[5]; //取出mac地址的后四个字节    deviceNameSuffix = macAddr - macAddrOrg + deviceNameOrgSuffix; //计算设备对应编号    name[7] = (deviceNameSuffix/10000)%10 + 0x30;    name[8] = (deviceNameSuffix/1000)%10 + 0x30;    name[9] = (deviceNameSuffix/100)%10 + 0x30;    name[10] = (deviceNameSuffix/10)%10 + 0x30;    name[11] = (deviceNameSuffix)%10 + 0x30;    name[12] = '/0';}

这里写图片描述

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台