-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathConvert.basic.h
179 lines (164 loc) · 7.59 KB
/
Convert.basic.h
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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2021, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file 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.
*
* This 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef __MQL__
#pragma once
#endif
// Includes.
#include "Array.mqh"
#include "Common.extern.h"
#include "DateTime.mqh"
#include "Std.h"
#include "String.mqh"
/**
* Class to provide conversion methods.
*/
class ConvertBasic {
public:
/**
* Convert integer to hex.
*/
static string IntToHex(long long_number) {
string result;
int integer_number = (int)long_number;
for (int i = 0; i < 4; i++) {
int byte = (integer_number >> (i * 8)) & 0xff;
result += StringFormat("%02x", byte);
}
return result;
}
/**
* Convert character into integer.
*/
static int CharToInt(ARRAY_REF(int, _chars)) {
return ((_chars[0]) | (_chars[1] << 8) | (_chars[2] << 16) | (_chars[3] << 24));
}
/**
* Assume: len % 4 == 0.
*/
static int String4ToIntArray(ARRAY_REF(int, output), string in) {
int len;
int i, j;
len = StringLen(in);
if (len % 4 != 0) len = len - len % 4;
int size = ArraySize(output);
if (size < len / 4) {
ArrayResize(output, len / 4);
}
for (i = 0, j = 0; j < len; i++, j += 4) {
output[i] = (StringGetCharacter(in, j)) | ((StringGetCharacter(in, j + 1)) << 8) |
((StringGetCharacter(in, j + 2)) << 16) | ((StringGetCharacter(in, j + 3)) << 24);
}
return (len / 4);
}
static void StringToType(string _value, bool& _out) {
#ifdef __MQL__
_out = _value != "" && _value != NULL && _value != "0" && _value != "false";
#else
_out = _value != "" && _value != "0" && _value != "false";
#endif
}
static void StringToType(string _value, int& _out) { _out = (int)StringToInteger(_value); }
static void StringToType(string _value, unsigned int& _out) { _out = (unsigned int)StringToInteger(_value); }
static void StringToType(string _value, char& _out) { _out = (char)_value[0]; }
static void StringToType(string _value, unsigned char& _out) { _out = (unsigned char)_value[0]; }
static void StringToType(string _value, long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, unsigned long& _out) { _out = StringToInteger(_value); }
static void StringToType(string _value, short& _out) { _out = (short)StringToInteger(_value); }
static void StringToType(string _value, unsigned short& _out) { _out = (unsigned short)StringToInteger(_value); }
static void StringToType(string _value, float& _out) { _out = (float)StringToDouble(_value); }
static void StringToType(string _value, double& _out) { _out = StringToDouble(_value); }
static void StringToType(string _value, string& _out) { _out = _value; }
static void StringToType(string _value, color& _out) { _out = 0; }
static void StringToType(string _value, datetime& _out) {
#ifdef __MQL4__
_out = StrToTime(_value);
#else
_out = StringToTime(_value);
#endif
}
template <typename X>
static X StringTo(string _value) {
X _out;
StringToType(_value, _out);
return _out;
}
static void BoolToType(bool _value, bool& _out) { _out = _value; }
static void BoolToType(bool _value, char& _out) { _out = (char)_value; }
static void BoolToType(bool _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void BoolToType(bool _value, int& _out) { _out = (int)_value; }
static void BoolToType(bool _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void BoolToType(bool _value, long& _out) { _out = (long)_value; }
static void BoolToType(bool _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void BoolToType(bool _value, short& _out) { _out = (short)_value; }
static void BoolToType(bool _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void BoolToType(bool _value, float& _out) { _out = (float)_value; }
static void BoolToType(bool _value, double& _out) { _out = (double)_value; }
static void BoolToType(bool _value, string& _out) { _out = _value ? "1" : "0"; }
static void BoolToType(bool _value, color& _out) { _out = 0; }
static void BoolToType(bool _value, datetime& _out) {}
template <typename X>
static X BoolTo(bool _value) {
X _out;
BoolToType(_value, _out);
return _out;
}
static void LongToType(long _value, bool& _out) { _out = (bool)_value; }
static void LongToType(long _value, char& _out) { _out = (char)_value; }
static void LongToType(long _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void LongToType(long _value, int& _out) { _out = (int)_value; }
static void LongToType(long _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void LongToType(long _value, long& _out) { _out = (long)_value; }
static void LongToType(long _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void LongToType(long _value, short& _out) { _out = (short)_value; }
static void LongToType(long _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void LongToType(long _value, float& _out) { _out = (float)_value; }
static void LongToType(long _value, double& _out) { _out = (double)_value; }
static void LongToType(long _value, string& _out) { _out = _value ? "1" : "0"; }
static void LongToType(long _value, color& _out) { _out = 0; }
static void LongToType(long _value, datetime& _out) {}
template <typename X>
static X LongTo(long _value) {
X _out;
LongToType(_value, _out);
return _out;
}
static void DoubleToType(double _value, bool& _out) { _out = (bool)_value; }
static void DoubleToType(double _value, char& _out) { _out = (char)_value; }
static void DoubleToType(double _value, unsigned char& _out) { _out = (unsigned char)_value; }
static void DoubleToType(double _value, int& _out) { _out = (int)_value; }
static void DoubleToType(double _value, unsigned int& _out) { _out = (unsigned int)_value; }
static void DoubleToType(double _value, long& _out) { _out = (long)_value; }
static void DoubleToType(double _value, unsigned long& _out) { _out = (unsigned long)_value; }
static void DoubleToType(double _value, short& _out) { _out = (short)_value; }
static void DoubleToType(double _value, unsigned short& _out) { _out = (unsigned short)_value; }
static void DoubleToType(double _value, float& _out) { _out = (float)_value; }
static void DoubleToType(double _value, double& _out) { _out = (double)_value; }
static void DoubleToType(double _value, string& _out) { _out = _value ? "1" : "0"; }
static void DoubleToType(double _value, color& _out) { _out = 0; }
static void DoubleToType(double _value, datetime& _out) {}
template <typename X>
static X DoubleTo(double _value) {
X _out;
DoubleToType(_value, _out);
return _out;
}
};