This repository was archived by the owner on Sep 16, 2024. It is now read-only.
File tree 2 files changed +43
-0
lines changed 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 1
1
#include "str_utils.h"
2
2
#include <stdio.h>
3
+ #include <string.h>
3
4
4
5
/**
5
6
* Create a string representation of a uint8
@@ -18,3 +19,43 @@ void sprint_binary_u8(char* s, uint8_t v){
18
19
);
19
20
}
20
21
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
+ }
Original file line number Diff line number Diff line change 14
14
15
15
void sprint_binary_u8 (char * s , uint8_t v );
16
16
17
+ void hexdump (const uint8_t * buf , size_t len );
18
+
17
19
#endif
You can’t perform that action at this time.
0 commit comments