forked from thanks4opensource/buck50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_to_hex.hxx
143 lines (119 loc) · 3.29 KB
/
bin_to_hex.hxx
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// buck50: Test and measurement firmware for “Blue Pill” STM32F103 development board
// Copyright (C) 2019,2020 Mark R. Rubin aka "thanks4opensource"
//
// This file is part of buck50.
//
// The buck50 program is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The buck50 program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// (LICENSE.txt) along with the buck50 program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#ifndef BIN_TO_HEX_HXX
#define BIN_TO_HEX_HXX
#include <stdint.h>
namespace bitops {
class BinToHex {
public:
const char* hex() const { return _hex; }
const char* uint4(
const uint8_t bin)
{
_hex[0] = hex(bin);
_hex[1] = '\0';
return _hex;
}
static const char* uint4(
const uint8_t bin,
char *hex)
{
*hex = BinToHex::hex(bin);
return hex;
}
const char* uint8(
const uint8_t bin)
{
byte(bin, 0, _hex);
_hex[2] = '\0';
return _hex;
}
static const char* uint8(
const uint8_t bin,
char *hex)
{
byte(bin, 0, hex);
return hex;
}
const char* uint16(
const uint16_t bin)
{
byte(bin >> 8, 0, _hex);
byte(bin & 0xff, 2, _hex);
_hex[4] = '\0';
return _hex;
}
static const char* uint16(
const uint16_t bin,
char *hex)
{
byte(bin >> 8, 0, hex);
byte(bin & 0xff, 2, hex);
return hex;
}
const char* uint32(
const uint32_t bin)
{
byte( bin >> 24 , 0, _hex);
byte((bin >> 16) & 0xff, 2, _hex);
byte((bin >> 8) & 0xff, 4, _hex);
byte( bin & 0xff, 6, _hex);
_hex[8] = '\0';
return _hex;
}
static const char* uint32(
const uint32_t bin,
char *hex)
{
byte( bin >> 24 , 0, hex);
byte((bin >> 16) & 0xff, 2, hex);
byte((bin >> 8) & 0xff, 4, hex);
byte( bin & 0xff, 6, hex);
return hex;
}
static uint32_t hex_to_bin(
const char* const hex,
const uint8_t maxlen = 8)
{
uint32_t bin = 0;
for (uint8_t ndx = 0 ; hex[ndx] != '\0' && ndx < maxlen ; ++ndx) {
uint8_t hexit = hex[ndx] - '0';
if (hexit > 9) hexit -= 'a' - ':';
bin = (bin << 4) | hexit;
}
return bin;
}
protected:
static void byte(
const uint8_t byte ,
const uint8_t position ,
char *hex_chars)
{
hex_chars[position ] = hex(byte >> 4 );
hex_chars[position + 1] = hex(byte & 0x0f);
}
static char hex(
const uint8_t nibble)
{
return '0' + nibble + (nibble > 9 ? 'a' - ':' : 0);
}
char _hex[8 + 1];
};
} // namespace bitops
#endif // #ifndef BIN_TO_HEX_HXX