C语言中字符串的形式打印16进制数据。 这样写,输出更直观些
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #define PRINT_BUF_MAX (32) #define TAG_STRING_MAX (32) static void print_step_hex_data(const char *tag, void *pbuf, size_t len) { int i; char buf[PRINT_BUF_MAX * 4] = {0}; char temp_buf[4] = {0}; sprintf(buf,"%s",tag); for (i=0;i<len;i++) { if(i % 16 ==0) strcat(buf,"\n"); sprintf(temp_buf," %02x",*((char *)pbuf + i)&0xff); strcat(buf, temp_buf); } strcat(buf,"\n"); LOGI("%s", buf); } void print_hex_data(const char *tag, void *pbuf, size_t len) { int i; uint32_t block; uint32_t remainder; void *p; char tag_buf[TAG_STRING_MAX + 6] = {0}; char tail_buf[6+1] = {0}; p = pbuf; if (len<=PRINT_BUF_MAX) return print_step_hex_data(tag, p, len); else { block = len / PRINT_BUF_MAX; remainder = len % PRINT_BUF_MAX; for (i=0;i<block;i++) { memcpy(tag_buf,tag,TAG_STRING_MAX); sprintf(tail_buf,"[%i]",i); strcat(tag_buf, tail_buf); print_step_hex_data(tag_buf, p, PRINT_BUF_MAX); p = (char *)p + PRINT_BUF_MAX; } if (remainder) { memcpy(tag_buf,tag,TAG_STRING_MAX); sprintf(tail_buf,"[%i]",i); strcat(tag_buf, tail_buf); print_step_hex_data(tag_buf, p, remainder); } } } |