-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversiontools.cpp
More file actions
55 lines (52 loc) · 1.28 KB
/
conversiontools.cpp
File metadata and controls
55 lines (52 loc) · 1.28 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
#include <QDebug>
#include "conversiontools.h"
// Converts decimal or hex number to integer. Returns -1 on error
uint64_t ConversionTools::numberStringToInteger(const QString str)
{
uint64_t res = 0;
int idx=0;
bool isHex = false;
if(str.startsWith("0x")) {
idx=2;
isHex = true;
}
int maxidx = str.size();
if(str.endsWith(("h"))) {
maxidx--;
isHex = true;
}
while (idx<maxidx) {
const QChar c = str.at(idx++);
if(!c.isDigit() &&
(!isHex ||
(
isHex &&
!(c >= 'a' && c <= 'f') &&
!(c >= 'A' && c <= 'F')
)
)
) {
qDebug() << "Number format incorrect:" << str;
return -1;
}
if (isHex)
res <<= 4;
else
res *= 10;
unsigned long ch = c.toLatin1();
if(c.isDigit())
ch -= '0';
else if(c <= 'F')
ch -= ('A'-10);
else
ch -= ('a'-10);
res += reinterpret_cast<uint64_t>(ch);
}
return res;
}
const QString ConversionTools::int64ToHexString(const uint64_t n)
{
QString res("0x");
res = res.append(QString::number(n,16));
return res;
}