hex_sprintf
Link to this paste: http://bugs.dragonflybsd.org/pastes/429
Added by tuxillo 6 months ago.
Syntax: Plain Text
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 |
antonioh@devel01:/home/antonioh/temp> ./t_hex
01:02:03:04:05:06
antonioh@devel01:/home/antonioh/temp> cat t_hex.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
static char digits[] = "0123456789abcdef";
char *
hex_sprintf(const u_char *ptr, char *buf, const char sep, int len)
{
int i;
if (buf == NULL || ptr == NULL)
return NULL;
for (i = 0; i < len; i++) {
*buf++ = digits[*ptr >> 4];
*buf++ = digits[*ptr++ & 0xf];
*buf++ = sep;
}
*--buf = 0;
return buf;
}
int
main(void)
{
u_char tst[6] = {1, 2, 3, 4, 5, 6};
/* 6 u_char x 3 byte to represent each with separator */
char *buf = malloc(6 * 3);
/* For len you pass the number of elements in the u_char array */
hex_sprintf(tst, buf, ':', 6);
printf("%s\n", buf);
return 0;
}
|