208 lines
6.5 KiB
MQL5
//+------------------------------------------------------------------+
| |||
//| ToStr.mqh |
| |||
//| Copyright 2026, Niquel Mendoza. |
| |||
//| https://www.mql5.com |
| |||
//+------------------------------------------------------------------+
| |||
#property copyright "Copyright 2026, Niquel Mendoza."
| |||
#property link "https://www.mql5.com"
| |||
#property strict
| |||
| |||
#ifndef TSNSTD_SRC_STRING_CONV_TOSTR_MQH
| |||
#define TSNSTD_SRC_STRING_CONV_TOSTR_MQH
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
#include "Def.mqh"
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
namespace TSN
| |||
{
| |||
//+------------------------------------------------------------------+
| |||
//| Maximo numero de digitos |
| |||
//+------------------------------------------------------------------+
| |||
//--- incluye el signo para i\l (+1)
| |||
#define MQLARTICLES_CONV_MAXDIG_I (11)
| |||
#define MQLARTICLES_CONV_MAXDIG_L (20)
| |||
| |||
//---
| |||
#define MQLARTICLES_CONV_MAXDIG_UL (20)
| |||
| |||
//---
| |||
// 16 + 1 + 1 + 4 (2 a la 64 divido entre 10 a la 16..)
| |||
// 6 si o si mas digitos
| |||
#define MQLARTICLES_CONV_MAXDIG_DBL(dig) (6 + dig)
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
class CValueToBuffer
| |||
{
| |||
public:
| |||
CValueToBuffer(void) {}
| |||
~CValueToBuffer(void) {}
| |||
| |||
//--- convertir enteros
| |||
//- unsigned
| |||
template <typename TUValue>
| |||
static void _ToValUSigned(TUValue uvalue, uchar& in[], int pos);
| |||
| |||
//---
| |||
static void ToValue(double v, uchar& in[], int& w, uchar digits);
| |||
| |||
//---
| |||
static void ULLToString(ulong uvalue, string& str, int pos);
| |||
};
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
static void CValueToBuffer::ToValue(double v, uchar &in[], int &w, uchar digits)
| |||
{
| |||
//--- signo
| |||
if(v < 0)
| |||
{
| |||
in[w++] = '-';
| |||
v = -v;
| |||
}
| |||
| |||
//---
| |||
const double factor = g_potencias_de_10[digits]; // potencias positivas
| |||
const ulong escalado = (ulong)MathRound(v * factor);
| |||
const ulong factor_u = (ulong)factor;
| |||
ulong ent = escalado / factor_u;
| |||
ulong dec = escalado - ent * factor_u;
| |||
| |||
//---
| |||
{
| |||
int pos = w + CBitTricks::CountDigitsBinary(ent);
| |||
w = pos;
| |||
| |||
//---
| |||
while(ent >= 100)
| |||
{
| |||
const ulong q = ent / 100ULL;
| |||
const ulong idx = (ent - q * 100ULL) << 1;
| |||
ent = q;
| |||
in[--pos] = g_tsn_digit_pairs[idx + 1];
| |||
in[--pos] = g_tsn_digit_pairs[idx];
| |||
}
| |||
if(ent >= 10)
| |||
{
| |||
const ulong idx = ent << 1ULL;
| |||
in[--pos] = g_tsn_digit_pairs[idx + 1];
| |||
in[--pos] = g_tsn_digit_pairs[idx];
| |||
}
| |||
else
| |||
in[--pos] = uchar('0' + ent);
| |||
}
| |||
| |||
//---
| |||
in[w++] = '.';
| |||
| |||
//---
| |||
int pos = w + digits;
| |||
w = pos;
| |||
| |||
//---
| |||
int i = 0;
| |||
for(; i + 1 < digits; i += 2)
| |||
{
| |||
const ulong q = dec / 100ULL;
| |||
const ulong idx = (dec - q * 100ULL) << 1;
| |||
dec = q;
| |||
in[--pos] = g_tsn_digit_pairs[idx + 1];
| |||
in[--pos] = g_tsn_digit_pairs[idx];
| |||
}
| |||
if(i < digits)
| |||
in[--pos] = uchar('0' + (dec % 10ULL));
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
// helper
| |||
#define STRCONV_VALTOBUFFER_USIGNED(uvalue, in, w) \
| |||
w += TSN::CBitTricks::CountDigitsBinary(uvalue); \
| |||
TSN::CValueToBuffer::_ToValUSigned(uvalue, in, w);
| |||
| |||
| |||
//+------------------------------------------------------------------+
| |||
#define STRCONV_VALTOBUFFER_SIGNED(value, in, w, TUValue, VAR) \
| |||
TUValue VAR; \
| |||
if(value < 0) \
| |||
{ \
| |||
in[w++] = '-'; \
| |||
VAR = TUValue(-(value + 1)) + 1; \
| |||
} \
| |||
else \
| |||
VAR = TUValue(value); \
| |||
STRCONV_VALTOBUFFER_USIGNED(VAR, in, w)
| |||
| |||
| |||
//+------------------------------------------------------------------+
| |||
// asume pos ya aumentado si no puede ser algo consistente (seria raro no?¿) use los macros
| |||
template <typename TUValue>
| |||
static void CValueToBuffer::_ToValUSigned(TUValue uvalue, uchar &in[], int pos)
| |||
{
| |||
//---
| |||
while(uvalue >= 100)
| |||
{
| |||
const TUValue prev = uvalue;
| |||
uvalue /= 100U;
| |||
const TUValue idx = (prev - (uvalue * 100U)) << 1;
| |||
in[--pos] = g_tsn_digit_pairs[idx + 1];
| |||
in[--pos] = g_tsn_digit_pairs[idx];
| |||
}
| |||
if(uvalue >= 10)
| |||
{
| |||
const TUValue idx = uvalue << 1;
| |||
in[--pos] = g_tsn_digit_pairs[idx + 1];
| |||
in[--pos] = g_tsn_digit_pairs[idx];
| |||
}
| |||
else
| |||
in[--pos] = uchar('0' + uvalue);
| |||
}
| |||
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| |
| |||
//+------------------------------------------------------------------+
| |||
// Extra para string
| |||
//---
| |||
// VALUE= entero
| |||
// uv= ulong
| |||
// lo que hace es "un alias" para añadir a un indice la logintud de UV...
| |||
#define STRCONV_VALTOBUFFER_ADDDIG(VALUE, UV) VALUE+=CBitTricks::CountDigitsBinary(UV);
| |||
| |||
//---
| |||
#define STRCONV_VALTOBUFFER_ULLSTR_WR(UV, STR, PREFIX) \
| |||
const int __PREFIX = TSN::CBitTricks::CountDigitsBinary(UV); \
| |||
StringInit(STR, __PREFIX, ' '); \
| |||
TSN::CValueToBuffer::ULLToString(UV, STR, __PREFIX);
| |||
| |||
//+------------------------------------------------------------------+
| |||
static void CValueToBuffer::ULLToString(ulong uvalue, string& str, int pos)
| |||
{
| |||
//---
| |||
while(uvalue >= 100)
| |||
{
| |||
const ulong prev = uvalue;
| |||
uvalue /= 100U;
| |||
const ulong idx = (prev - (uvalue * 100U)) << 1;
| |||
str.SetChar(--pos, g_tsn_digit_pairs[idx + 1]);
| |||
str.SetChar(--pos, g_tsn_digit_pairs[idx]);
| |||
}
| |||
if(uvalue >= 10)
| |||
{
| |||
const ulong idx = uvalue << 1;
| |||
str.SetChar(--pos, g_tsn_digit_pairs[idx + 1]);
| |||
str.SetChar(--pos, g_tsn_digit_pairs[idx]);
| |||
}
| |||
else
| |||
str.SetChar(--pos, uchar('0' + uvalue));
| |||
}
| |||
}
| |||
#endif // TSNSTD_SRC_STRING_CONV_TOSTR_MQH
|