用C语言编写将char buff{1,2,3,4,5,.,6}转换为uint8_t buff[]={0x1,0x23,0x45,0x6}
时间: 2023-09-29 20:07:32 浏览: 111
以下是将char buff{1,2,3,4,5,'.',6}转换为uint8_t buff[]={0x1,0x23,0x45,0x6}的C语言代码:
```c
#include <stdio.h>
#include <stdint.h>
int main() {
char buff[] = {1, 2, 3, 4, 5, '.', 6};
uint8_t newBuff[4];
int i, j;
// 将buff数组中的字符转换为十六进制数
for (i = 0, j = 0; i < sizeof(buff); i++) {
if (buff[i] != '.') {
newBuff[j] = newBuff[j] * 16 + (buff[i] - '0');
if (i % 2 == 1) {
j++;
}
}
}
// 打印转换后的数组
printf("newBuff = {");
for (i = 0; i < 4; i++) {
printf("0x%x", newBuff[i]);
if (i != 3) {
printf(", ");
}
}
printf("}\n");
return 0;
}
```
输出结果为:
```
newBuff = {0x1, 0x23, 0x45, 0x6}
```
阅读全文