-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex.c
47 lines (39 loc) · 811 Bytes
/
hex.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include "hex.h"
int
hexvalid(const char *hexbuf, int len)
{
int i;
for (i=0; i < len; i++)
if (!isxdigit(hexbuf[i]))
return 0;
return 1;
}
void
hexparse(uint8_t *buf, const char *hexbuf, int len)
{
int i;
char tmp[3] = {0};
for (i=0; i < len; i++) {
tmp[0] = hexbuf[0];
tmp[1] = hexbuf[1];
*buf++ = strtoul(tmp, NULL, 16);
hexbuf += 2;
}
}
void
hexfmt(char *obuf, int olen, const char *ibuf, int ilen)
{
int i;
if (olen <= 0)
return;
// ensure that we don't overflow obuf
if (ilen >= olen/3)
ilen = (olen-1)/3;
for (i=0; i < ilen; i++)
snprintf(obuf + i*3, 4, "%02x ", ibuf[i]);
obuf[i*3] = 0;
}