-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhex.c
60 lines (51 loc) · 1.38 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stddef.h>
#include "hex.h"
int hextoa(char* const a, size_t alen, const char* const hex, size_t hexlen) {
size_t j = alen - (hexlen / 2);
for (size_t i = 0; i < hexlen; ++i) {
char c;
if (hex[i] >= 0x61) { // Lowercase letter
c = (char)((hex[i] - 0x57) << 4);
} else if (hex[i] >= 0x41) { // Uppercase letter
c = (char)((hex[i] - 0x37) << 4);
} else { // Number
c = (char)((hex[i] - 0x30) << 4);
}
++i;
if (hex[i] >= 0x61) {
c += hex[i] - 0x57;
} else if (hex[i] >= 0x41) {
c += hex[i] - 0x37;
} else {
c += hex[i] - 0x30;
}
a[j] = c;
++j;
}
return 0;
}
int atohex(char* const hex, const char* const a, size_t len) {
size_t j = 0;
for (size_t i = 0; i < len; ++i) {
// Skip if the byte is a leading 0 unless we're on the last byte.
if (a[i] == 0 && j == 0 && (i != len - 1)) {
continue;
}
char high = a[i] >> 4;
char low = a[i] & 0x0F;
if (high <= 0x9) {
hex[j] = high + 0x30;
} else {
hex[j] = high + 0x57;
}
++j;
if (low <= 0x9) {
hex[j] += low + 0x30;
} else {
hex[j] += low + 0x57;
}
++j;
}
hex[j] = '\0';
return 0;
}