8000 str_utils: add hexdump() · pycom/pycom-micropython-sigfox@7d2e8e0 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 7d2e8e0

Browse files
committed
str_utils: add hexdump()
1 parent b822a70 commit 7d2e8e0

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

esp32/util/str_utils.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "str_utils.h"
22
#include <stdio.h>
3+
#include <string.h>
34

45
/**
56
* Create a string representation of a uint8
@@ -18,3 +19,43 @@ void sprint_binary_u8(char* s, uint8_t v){
1819
);
1920
}
2021

22+
/* hexdump a buffer
23+
*/
24+
void hexdump(const uint8_t* buf, size_t len){
25+
const size_t line_len = 16;
26+
uint8_t line[line_len+1];
27+
memset(line, ' ', line_len);
28+
line[line_len] = '\0';
29+
30+
for ( size_t i = 0; i < len; ++i) {
31+
uint8_t c = buf[i];
32+
printf("%02x ", c);
33+
if ( (c >= (uint8_t)'a' && c <= (uint8_t)'z')
34+
|| (c >= (uint8_t)'A' && c <= (uint8_t)'Z')
35+
|| (c >= (uint8_t)'0' && c <= (uint8_t)'9') )
36+
{
37+
line[i%line_len] = c;
38+
} else {
39+
line[i%line_len] = '.';
40+
}
41+
42+
// space after 8 bytes
43+
if (i%16 == 7)
44+
printf(" ");
45+
// end of line after 16 bytes
46+
if (i%16==15){
47+
printf(" |%s|\n", line);
48+
memset(line, ' ', line_len);
49+
}
50+
}
51+
if ( len % line_len ){
52+
// space after 8 bytes
53+
if ( len % line_len < 7)
54+
printf(" ");
55+
// spaces for bytes we didn't have to print
56+
for ( size_t j = line_len; j > len % line_len; j-- ){
57+
printf(" ");
58+
}
59+
printf(" |%s|\n", line);
60+
}
61+
}

esp32/util/str_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414

1515
void sprint_binary_u8(char* s, uint8_t v);
1616

17+
void hexdump(const uint8_t* buf, size_t len);
18+
1719
#endif

0 commit comments

Comments
 (0)
0