Convert String to Integer

 

I want to convert symbol name (string) ?to Integer so for each symbol its a unique int and it can be encoded and decoded

base64 is too long and may contain string also, so any other method please inform

 
Arpit T: I want to convert symbol name (string) ?to Integer so for each symbol its a unique int and it can be encoded and decoded. base64 is too long and may contain string also, so any other method please inform

Convert it base10 then, but you will have to write your own conversion function for it. There are many such implementations on the web.

You can also convert to base16 as the hexadecimal representation of an integer.

Both methods will work.

 
You can use hash functions: here is an article about it: https://www.mql5.com/en/articles/1334#c2_4
 
I use a union to do such for generating magic numbers per symbol.

Create a union with an ulong and a char array of size 8. Insert the string into the array by StringToCharArray.

There is a little more detail to it, depending on the symbol scheme your broker uses, but in general, it works quite well.


 

Double to Integer, I'm deeply sorry:


   IleZerPoPrz=Digits();

            CenaAskStStr=DoubleToString(Tab2_CenaAsk[LiczKonw-1],IleZerPoPrz);

            DlCenaAskStStr=StringLen(CenaAskStStr);

            CenaAskStStrBK="";

            for(PozStr=0; PozStr<DlCenaAskStStr; PozStr++){

               ZnakStr=StringSubstr(CenaAskStStr,PozStr,1);

               if(ZnakStr==".") continue;

               CenaAskStStrBK=CenaAskStStrBK+ZnakStr;}

            CenaAskStLong=StringToInteger(CenaAskStStrBK);

 

String to Integer, from MQL5 Reference:


long  StringToInteger(string  value);


Great.  Before the dot.  And probably any character.

 
Paolo_ecn #:

String to Integer, from MQL5 Reference:


long  StringToInteger(string  value);


Great.  Before the dot.  And probably any character.

None of your anwers make any sense to the original question. - Or am I missing something here?

Anyways, post code correctly, its horrible to read.

 

Here is what I am doing, although I use a custom "Symbol()" function which does some magic to  the string....


#define EA_GLOBAL_MAGICID       0x00A02468

namespace fw_cfg
{
    const static union str_ulong_union 
    { 
        uchar s[8]; 
        ulong id; 
        str_ulong_union(const string _str) 
        { 
            StringToCharArray(_str, s); 
            id = (id << 16) + (PeriodSeconds()) + EA_GLOBAL_MAGICID + (AccountInfoInteger(ACCOUNT_LOGIN)) + id; 
            for(int cnt = NULL; cnt < 8; cnt++) 
            { s[cnt] = (s[cnt] % 62) + 64; } 
        }
        string str() const
        { return((CharArrayToString(s, 0, 8))); }
    } exp_magic_UID(Symbol());
}; // END Namespace

#define SYMBOL_MAGIC_ID   fw_cfg::exp_magic_UID.id


This should give you a unique MAGIC ID for each symbol, differentiated by timeframe and for every account. This way you can use the magic ID to identify positions when stored in a file or sql database. 

As far as I was able to test this, it was unique and had no overlappings within my DB.

Reason: