-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathConvert.cpp
More file actions
183 lines (160 loc) · 3.2 KB
/
Copy pathConvert.cpp
File metadata and controls
183 lines (160 loc) · 3.2 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Convert.cpp
*
* Created on: 24 May 2010
* Author: orac7
*/
#include "Convert.h"
#include <mavsprintf.h>
#undef HAVE_SNPRINTF
#include "snprintf.h"
#include <mawstring.h>
#include <wchar.h>
int Convert::toInt(const char* digit)
{
int sign = 1;
int result = 0;
// check sign
if (*digit == '-')
{
sign = -1;
digit++;
}
//--- get integer portion
while (*digit >= '0' && *digit <='9')
{
result = (result * 10) + *digit-'0';
digit++;
}
//--- set to sign given at the front
return result * sign;
}
int Convert::toInt(String& input)
{
return toInt(input.c_str());
}
double Convert::toDouble(String& input)
{
return toDouble(input.c_str());
}
double Convert::toDouble(const char* input)
{
int sign = 1;
double result = 0;
const char* digit = input;
// check sign
if (*digit == '-')
{
sign = -1;
digit++;
}
//--- get integer portion
while (*digit >= '0' && *digit <='9')
{
result = (result * 10) + *digit-'0';
digit++;
}
//--- get decimal point and fraction, if any.
if (*digit == '.')
{
digit++;
double scale = 0.1;
while (*digit >= '0' && *digit <='9') {
result += (*digit-'0') * scale;
scale *= 0.1;
digit++;
}
}
//--- error if we're not at the end of the number
if (*digit != 0) {
return 0.00;
}
//--- set to sign given at the front
return result * sign;
}
time_t Convert::toDateTime(String& input)
{
return (time_t)toInt(input);
}
String Convert::toString(bool input)
{
if(input)
return "1";
else
return "0";
}
String Convert::toString(int input)
{
char buffer[128];
snprintf(buffer, 128, "%d", input);
return buffer;
}
String Convert::toString(double input)
{
char buffer[128];
snprintf(buffer, 128, "%f", input);
return buffer;
}
String Convert::toString(const byte* src, size_t count)
{
//At least enough space, even if every byte turns out to be a single character
char output[count + 1];
int ctr = 0;
while(*src != 0)
{
if((*src & 0x80) == 0)
{
//ASCII-compatible character
output[ctr++] = *src++;
}
else if((*src & 0xE0) == 0xC0)
{
// 2 bytes
byte b1 = *src++;
byte b2 = *src++;
if((b2 & 0xC0) != 0x80)
break;
output[ctr++] = ((b1 & 0x1F) << 6) | (b2 & 0x3F);
}
else if((*src & 0xF0) == 0xE0)
{
// 3 bytes
byte b1 = *src++;
byte b2 = *src++;
byte b3 = *src++;
if((b2 & 0xC0) != 0x80)
break;
if((b3 & 0xC0) != 0x80)
break;
output[ctr++] = ((b1 & 0x0F) << 12) | ((b2 & 0x1F) << 6) |
(b3 & 0x3F);
}
}
output[ctr] = '\0';
return output;
}
int Convert::hexToInt(const char* input)
{
int v = 0;
while (char c = *input++)
{
if (c < '0') return 0; //invalid character
if (c > '9') //shift alphabetic characters down
{
if (c >= 'a') c -= 'a' - 'A'; //upper-case 'a' or higher
if (c > 'Z') return 0; //invalid character
if (c > '9') c -= 'A'-1-'9'; //make consecutive with digits
if (c < '9' + 1) return 0; //invalid character
}
c -= '0'; //convert char to hex digit value
v = v << 4; //shift left by a hex digit
v += c; //add the current digit
}
return v;
}
String Convert::intToHex(int input)
{
char buffer[128];
snprintf(buffer, 128, "%08X", input);
return buffer;
}