xref: /freebsd/share/examples/libusb20/util.c (revision abd87254)
1 /* ----------------------------------------------------------------------------
2  * "THE BEER-WARE LICENSE" (Revision 42) (by Poul-Henning Kamp):
3  * <joerg@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
4  * can do whatever you want with this stuff. If we meet some day, and you think
5  * this stuff is worth it, you can buy me a beer in return.        Joerg Wunsch
6  * ----------------------------------------------------------------------------
7  */
8 
9 /*
10  * Helper functions common to all examples
11  */
12 
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 
17 #include <libusb20.h>
18 #include <libusb20_desc.h>
19 
20 #include "util.h"
21 
22 /*
23  * Print "len" bytes from "buf" in hex, followed by an ASCII
24  * representation (somewhat resembling the output of hd(1)).
25  */
26 void
27 print_formatted(uint8_t *buf, uint32_t len)
28 {
29   int i, j;
30 
31   for (j = 0; j < len; j += 16)
32     {
33       printf("%02x: ", j);
34 
35       for (i = 0; i < 16 && i + j < len; i++)
36 	printf("%02x ", buf[i + j]);
37       printf("  ");
38       for (i = 0; i < 16 && i + j < len; i++)
39 	{
40 	  uint8_t c = buf[i + j];
41 	  if(c >= ' ' && c <= '~')
42 	    printf("%c", (char)c);
43 	  else
44 	    putchar('.');
45 	}
46       putchar('\n');
47     }
48 }
49