forked from KoynovStas/QCRC_Calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hextobytes.cpp
67 lines (40 loc) · 1.02 KB
/
hextobytes.cpp
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
#include "hextobytes.h"
HexToBytes::HexToBytes(QObject *parent) :
QObject(parent),
revers_chunk(false),
revers_data(false),
//private
token_rx("(\\w+)"),
hex_rx("^(0x)?([0-9a-f]+)$"),
num_words(0)
{
}
int HexToBytes::str_to_bytes(const QString& str)
{
bytes.clear();
num_words = 0;
int pos = 0;
while( (pos = token_rx.indexIn(str, pos)) != -1 )
{
pos += token_rx.matchedLength();
if( token_to_bytes(token_rx.cap(1)) == -1 )
return -1;
}
if( revers_data )
std::reverse(bytes.begin(), bytes.end());
return 0; //good job
}
int HexToBytes::token_to_bytes(const QString& token)
{
if( hex_rx.indexIn(token.toLower()) == -1 )
{
emit error("bad format: " + token);
return -1;
}
token_bytes = QByteArray::fromHex(hex_rx.cap(2).toLatin1());
num_words++;
if( revers_chunk )
std::reverse(token_bytes.begin(), token_bytes.end());
bytes += token_bytes;
return 0; //good job
}