帮我写一个C语言函数,把字符串C4:CB:54:08:EA:57,去除冒号,转换成6个16进制数字
时间: 2024-05-15 12:12:07 浏览: 66
将字符串转会为十六进制
以下是一个简单的实现:
```c
#include <stdio.h>
#include <string.h>
void convertMacAddress(char* macAddress, unsigned char* hexDigits) {
int i, j;
char buf[3] = {0};
for (i = 0, j = 0; i < strlen(macAddress); i++) {
if (macAddress[i] != ':') {
buf[j++] = macAddress[i];
}
if (j == 2) {
sscanf(buf, "%x", &hexDigits[i/2]);
j = 0;
}
}
}
int main() {
char macAddress[] = "C4:CB:54:08:EA:57";
unsigned char hexDigits[6] = {0};
convertMacAddress(macAddress, hexDigits);
printf("%02X %02X %02X %02X %02X %02X\n", hexDigits[0], hexDigits[1], hexDigits[2], hexDigits[3], hexDigits[4], hexDigits[5]);
return 0;
}
```
输出结果为:
```
C4 CB 54 08 EA 57
```
阅读全文