forked from walmis/stm32-slcan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
41 lines (33 loc) · 1014 Bytes
/
utils.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
/* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return
* Gerhard Bertelsmann
* ----------------------------------------------------------------------------
*/
#include "stm32-slcan.h"
void bin2hex(uint8_t *dst, unsigned char c) {
uint8_t nibble;
nibble = ((c & 0xf0) >> 4) + '0';
if (nibble >= 0x3a)
nibble += 7;
*dst++ = nibble;
nibble = (c & 0x0f) + '0';
if (nibble >= 0x3a)
nibble += 7;
*dst = nibble;
}
uint8_t nibble2bin(uint8_t s) {
if (s >= '0' && s <= '9')
return s - '0';
if (s >= 'A' && s <= 'F')
return s - 'A' + 10;
return s - 'a' + 10;
}
uint8_t hex2bin(char *s) {
uint8_t x;
x = nibble2bin(*s++) << 4;
x |= nibble2bin(*s);
return x;
}