首页 > 代码库 > MAC

MAC

void HexStrToByte(const char *src, unsigned char *dest, int srcLen)

{

short i;

unsigned char highByte, lowByte;


for (i = 0; i < srcLen; i += 2)

{

highByte = toupper(src[i]);

lowByte  = toupper(src[i + 1]);


if (highByte > 0x39)

highByte -= 0x37;

else

highByte -= 0x30;


if (lowByte > 0x39)

lowByte -= 0x37;

else

lowByte -= 0x30;


dest[i / 2] = (highByte << 4) | lowByte;

}

return;

}




unsigned short calc_crc( unsigned char *ptr, int len)

{

  unsigned short crc1 = 0,da=0,bbb =0;


    while ( len-- != 0 )

    {

        da = ( crc1 >> 8 ) & 0x00ff;    //8??????????CRC??8?

        crc1 <<= 8;                            //?8?,???CRC??8???

        crc1 ^= ccitt_table[ ( da ^ ( *ptr ) )];        // ?8?????????????CRC ,??????CRC

        ptr++;

    }


    return crc1;


int YD_Crc16(char *source,int len,char *mac)

{

        char    dest[1000] = {0};

        int      nret = 0;


HexStrToByte(source, dest,  len);

nret =  calc_crc(dest,len/2);

sprintf(mac,"%04X",nret);

memcpy(&mac[4],"0000",4);

}


MAC