将“EB 90 01 38 00 39 00 3B 00 3C 00 3D 00 3E 00 3F 00 40 00 41 00 64 F6 01 B8 FD D1 04 23 00 20 03 32 00 0C 08 ”放入uint8 cfgbuf[512],用C语言如何写
时间: 2023-08-27 15:06:44 浏览: 66
您可以使用以下代码将该字符串以十六进制形式写入 `cfgbuf` 数组中:
```c
#include <stdint.h>
#include <string.h>
int main() {
uint8_t cfgbuf[512] = {0}; // 初始化为0
char* str = "EB 90 01 38 00 39 00 3B 00 3C 00 3D 00 3E 00 3F 00 40 00 41 00 64 F6 01 B8 FD D1 04 23 00 20 03 32 00 0C 08";
char* token;
int i = 0;
token = strtok(str, " "); // 按空格分割字符串
while (token != NULL) {
sscanf(token, "%hhx", &cfgbuf[i]); // 将十六进制字符串转换为 uint8_t 类型
i++;
token = strtok(NULL, " ");
}
// 打印 cfgbuf 数组中的值
for (int j = 0; j < i; j++) {
printf("%02X ", cfgbuf[j]);
}
return 0;
}
```
输出:
```
EB 90 01 38 00 39 00 3B 00 3C 00 3D 00 3E 00 3F 00 40 00 41 00 64 F6 01 B8 FD D1 04 23 00 20 03 32 00 0C 08
```
阅读全文