-
Notifications
You must be signed in to change notification settings - Fork 3
/
hexString.c
72 lines (63 loc) · 1.6 KB
/
hexString.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* hexString.c
*
* Created on: Sep 13, 2014
* Author: thesquid
*/
/*
* hexString.c
* byteutils
*
* Created by Richard Murphy on 3/7/10.
* Copyright 2010 McKenzie-Murphy. All rights reserved.
*
*/
#include "hexString.h"
/* utility function to convert hex character representation to their nibble (4 bit) values */
static uint8_t
nibbleFromChar(char c)
{
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
return 255;
}
/* Convert a string of characters representing a hex buffer into a series of bytes of that real value */
uint8_t
*hexStringToBytes(char *inhex)
{
uint8_t *retval;
uint8_t *p;
int len, i;
len = strlen(inhex) / 2;
retval = malloc(len+1);
for(i=0, p = (uint8_t *) inhex; i<len; i++) {
retval[i] = (nibbleFromChar(*p) << 4) | nibbleFromChar(*(p+1));
p += 2;
}
retval[len] = 0;
return retval;
}
static char byteMap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
static int byteMapLen = sizeof(byteMap);
/* Utility function to convert nibbles (4 bit values) into a hex character representation */
static char
nibbleToChar(uint8_t nibble)
{
if(nibble < byteMapLen) return byteMap[nibble];
return '*';
}
/* Convert a buffer of binary values into a hex string representation */
char
*bytesToHexString(uint8_t *bytes, size_t buflen)
{
char *retval;
int i;
retval = malloc(buflen*2 + 1);
for(i=0; i<buflen; i++) {
retval[i*2] = nibbleToChar(bytes[i] >> 4);
retval[i*2+1] = nibbleToChar(bytes[i] & 0x0f);
}
retval[i] = '\0';
return retval;
}