forked from nique_372/TSNStd
149 lines
5 KiB
MQL5
149 lines
5 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| HexUtils.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_ENCODING_HEXUTILS_MQH
| |
#define TSNSTD_SRC_ENCODING_HEXUTILS_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include <TSN\\Tables\\ParsersBasic.mqh>
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class CHexUtils
| |
{
| |
public:
| |
CHexUtils(void) {}
| |
~CHexUtils(void) {}
| |
| |
//---
| |
// in = entrada
| |
// i = offset incial de lectura
| |
// len = tamaño de elemtnos a leer
| |
// buf = buffer de salida
| |
// w = offset en el buffer de salida
| |
// 1. No se valida si len es el que dice ser (osea si es mayor que el in.. ni nada se asume input) valido lo mismo con i\w... etc..
| |
static int HexToBytes(const uchar& in[], int i, int len, uchar& buf[], int w = 0);
| |
static int HexToBytes(const string& in, int i, int len, uchar& buf[], int w = 0);
| |
| |
//---
| |
// raw = bytes
| |
// t = tamaño de los bytes
| |
// out = string de salida
| |
// raw_i = offset de lecutra de raw
| |
static void BytesToHexUpper(const uchar &raw[], int t, string& out, int raw_i = 0);
| |
static void BytesToHexLower(const uchar &raw[], int t, string& out, int raw_i = 0);
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
static int CHexUtils::HexToBytes(const uchar& in[], int i, int len, uchar &buf[], int w = 0)
| |
{
| |
//---
| |
const int sob = len & 1;
| |
int exact_len = len - sob;
| |
const int req = (len >> 1) + sob;
| |
| |
//---
| |
if(ArraySize(buf) < req)
| |
ArrayResize(buf, req);
| |
| |
//---
| |
exact_len += i; // para que peuda empzar bien..
| |
| |
| |
//---
| |
if(sob)
| |
{
| |
buf[w++] = g_arr_valid_hex_chars[in[i++]];
| |
exact_len++;
| |
}
| |
//---
| |
for(; i < exact_len; i += 2)
| |
buf[w++] = (g_arr_valid_hex_chars[in[i]] << 4) | g_arr_valid_hex_chars[in[i + 1]];
| |
| |
//---
| |
return w;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
static int CHexUtils::HexToBytes(const string &in, int i, int len, uchar &buf[], int w = 0)
| |
{
| |
//---
| |
const int sob = len & 1;
| |
int exact_len = len - sob;
| |
const int req = (len >> 1) + sob;
| |
| |
//---
| |
if(ArraySize(buf) < req)
| |
ArrayResize(buf, req);
| |
| |
//---
| |
exact_len += i; // para que peuda empzar bien..
| |
| |
//---
| |
if(sob)
| |
{
| |
buf[w++] = g_arr_valid_hex_chars[(uchar)in[i++]];
| |
exact_len++;
| |
}
| |
//---
| |
for(; i < exact_len; i += 2)
| |
buf[w++] = (g_arr_valid_hex_chars[(uchar)in[i]] << 4) | g_arr_valid_hex_chars[(uchar)in[i + 1]];
| |
| |
//---
| |
return w;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#define MQLARTICLES_UENCODE_HEXUTILS_BYTEST \
| |
{ \
| |
StringInit(out, t << 1, ' '); \
| |
int w = 0; \
| |
t+=raw_i; \
| |
for(; raw_i < t; raw_i++) \
| |
{ \
| |
const uchar num = raw[raw_i]; \
| |
out.SetChar(w++,digitos[num >> 4]); \
| |
out.SetChar(w++,digitos[num & 15]); \
| |
} \
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
static void CHexUtils::BytesToHexUpper(const uchar &raw[], int t, string& out, int raw_i = 0)
| |
{
| |
//---
| |
static const uchar digitos[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
| |
MQLARTICLES_UENCODE_HEXUTILS_BYTEST
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
static void CHexUtils::BytesToHexLower(const uchar &raw[], int t, string& out, int raw_i = 0)
| |
{
| |
//---
| |
static const uchar digitos[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
| |
MQLARTICLES_UENCODE_HEXUTILS_BYTEST
| |
}
| |
| |
}
| |
//+------------------------------------------------------------------+
| |
| |
| |
#endif // TSNSTD_SRC_ENCODING_HEXUTILS_MQH
| |
//+------------------------------------------------------------------+
|