69,373
社区成员
发帖
与我相关
我的任务
分享
[User:root Time:21:23:37 Path:/home/liangdong/c]$ make
gcc -g -I./include -c -o src/main.o src/main.c
gcc -o output src/main.o -lpthread -lm -lz
Makefile done.
[User:root Time:21:23:37 Path:/home/liangdong/c]$ ./output
0x12 0x7 0x6 0x17 0x10 0x23 0x20
[User:root Time:21:23:38 Path:/home/liangdong/c]$ cat src/main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int strpuint8(const char *time, uint8_t *buf) {
if (!time) {
return -1;
}
int i;
for (i = 0; i != 7; ++ i) {
if (sscanf(time + i * 2, "%2x", buf + i) != 1) {
return -1;
}
}
return 0;
}
int main(int argc, char* const argv[]) {
uint8_t set_time_buf[7];
const char *time = "12070617102320";
int ret = strpuint8(time, set_time_buf);
if (ret == 0) {
int i;
for (i = 0; i != 7; ++ i) {
printf("0x%x ", set_time_buf[i]);
}
}
printf("\n");
return 0;
}
#include <stdio.h>
#define GET_NUM(ch) ((ch) - '0')
unsigned char* set_buf(const char* str, unsigned char* buf, const size_t LEN) {
size_t i;
unsigned char num = 0;
for (i = 0; i < LEN; ++i) {
num = GET_NUM(str[2*i]) * 10 + GET_NUM(str[2*i+1]);
buf[i] = num;
}
return buf;
}
/* 测试代码 */
int main() {
const char *str = "120617102320";
size_t i;
unsigned char buf[7];
const size_t LEN = sizeof(buf);
set_buf(str, buf, 7);
for (i = 0; i < LEN; ++i) {
printf("%d\n", buf[i]);
}
return 0;
}