编辑代码

#include <stdio.h>
#include <string.h>
int rtk_string_to_hex(const char *string, unsigned char *key, int len)
{
	char tmpBuf[4];
	int idx, ii = 0;

	if (string == NULL || key == NULL)
		return 0;

	for (idx = 0; idx < len; idx += 2) {
		tmpBuf[0] = string[idx];
		tmpBuf[1] = string[idx + 1];
		tmpBuf[2] = 0;

		// if (!isxdigit(tmpBuf[0]) || !isxdigit(tmpBuf[1]))
		// 	return 0;

		key[ii++] = strtoul(tmpBuf, NULL, 16);
	}

	return 1;
}
int isValidMacAddr(unsigned char *macAddr)
{
	// Check for bad, multicast, broadcast, or null address
	if ((macAddr[0] & 1) || (macAddr[0] & macAddr[1] & macAddr[2] & macAddr[3] & macAddr[4] & macAddr[5]) == 0xff
		|| (macAddr[0] | macAddr[1] | macAddr[2] | macAddr[3] | macAddr[4] | macAddr[5]) == 0x00)
		return 0;

	return 1;
}

int mac17ToMac6(char *mac17, char *mac6)
{
	int i;

	for (i=0; i<17; i++){
		if ((i+1)%3 != 0)
			mac17[i-(i+1)/3] = mac17[i];
	}
	mac17[12] = '\0';
	if (strlen(mac17) != 12  || !rtk_string_to_hex(mac17, mac6, 12) || !isValidMacAddr(mac6)) {
		// printf(strInvdMACAddr);
		return -1;
	}

	return 0;
}

int libgc_sys_setModel(uint8_t *value)
{
	return libgc_sys_setValue("Model",value);
}


int main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
    char mac[18] = {0};
    unsigned char hexmac[6] = {0};
    strcpy(mac,"aa:bb:cc:dd:ee:ff");
    
    mac17ToMac6(mac, hexmac);
    printf("mac = %s\n",mac);
    printf("hexmac = %02x\n",hexmac[0]);
    return 0;
}